Skip to content
Snippets Groups Projects
Commit cc98f6d3 authored by Akex's avatar Akex Committed by Александр Плохих
Browse files

make controller

parent 295a6342
No related branches found
No related tags found
1 merge request!9make controller
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Hello World!
Hello World !!
Hello World !!!
</body>
</html>
\ No newline at end of file
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use function App\Repository\readFileLineByLine;
class ReadFileLineByLineController extends AbstractController
{
/**
* Принимает путь до файла,
* проверяет, что файл существует и выводит пользователю построчный вывод используя yield
*
* @param string $filePath путь до файла
* @return Response */
#[Route('/readbyline/{filePath}', name: 'app_read_file_line_by_line')]
public function index(string $filePath) : Response
{
$file = "";
try{
foreach (readFileLineByLine($filePath) as $line) {
$file .= $line;
}
} catch (\Exception $exception) {
return new Response($exception->getMessage(), Response::HTTP_NOT_FOUND);
}
return new Response($file, Response::HTTP_OK);
}
}
<?php
namespace App\Repository;
use Exception;
use Generator;
/**
* Принимает путь до файла,
* проверяет, что файл существует и выводит пользователю построчный вывод используя yield
*
* @param string $filePath путь до файла
* @return Generator
* @throws Exception если казанного фала нет
*/
function readFileLineByLine(string $filePath): Generator
{
if (!file_exists($filePath)){
throw new Exception("неверный путь");
}
$file = fopen($filePath, 'r');
while (!feof($file)){
yield fgets($file);
}
fclose($file);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment