Skip to content
Snippets Groups Projects
HomeController.php 1.04 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
{
Адлан Шамавов's avatar
Адлан Шамавов committed
    private function readLogFile(string $fileName): string {
        $filePath = $this->getParameter('kernel.project_dir') . "/public/files/" . $fileName;
        if (file_exists($filePath)) {
            $text = "";
            $file = fopen($filePath, "r");
            while(!feof($file)) {
                $line = fgets($file);
                $text .= $line;
            }
            fclose($file);
            return $text;
        }
        return "Такого файла не существует.";
Адлан Шамавов's avatar
Адлан Шамавов committed
    }

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

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