Skip to content
Snippets Groups Projects
HomeController.php 1.05 KiB
Newer Older
Адлан Шамавов's avatar
Адлан Шамавов committed
<?php

namespace App\Controller;

use DateInterval;
use DatePeriod;
use DateTime;
Адлан Шамавов's avatar
Адлан Шамавов committed
use DateTimeImmutable;
Адлан Шамавов's avatar
Адлан Шамавов committed
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class HomeController extends AbstractController
{
    private function readFileLineByLine(string $fileName): iterable  {
Адлан Шамавов's avatar
Адлан Шамавов committed
        $filePath = $this->getParameter('kernel.project_dir') . "/public/files/" . $fileName;
        if (file_exists($filePath)) {
            $file = fopen($filePath, "r");
            while(!feof($file)) {
                yield fgets($file);
Адлан Шамавов's avatar
Адлан Шамавов committed
            }
            fclose($file);
        }
        else yield "Такого файла не существует.";
Адлан Шамавов's avatar
Адлан Шамавов committed
    }

Адлан Шамавов's avatar
Адлан Шамавов committed

    #[Route('/{fileName}', name: 'home')]
    public function home(string $fileName): Response // text.txt
        $text = "";
        foreach($this->readFileLineByLine($fileName) as $line) {
            $text .= $line;
        }
Адлан Шамавов's avatar
Адлан Шамавов committed
        return $this->render('home.html.twig', ['text' => $text]);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
}