src/Controller/SiteController.php line 41
<?php
namespace App\Controller;
use App\Repository\ContentRepository;
use App\Repository\MenuRepository;
use App\Repository\UsersCommentRepository;
use App\Repository\UsersRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class SiteController extends AbstractController
{
private $display = [];
function __construct(MenuRepository $menuRepository, UsersRepository $usersRepository,private RequestStack $request)
{
$menuList = $menuRepository->findBy(['controller'=>'site','user_logged'=>0],['ord'=>'desc']);
$this->addDisplay('menuList',$menuList);
#$this->addDisplay("action",$request->getCurrentRequest()->get)
if($request->getSession()->get("userid")){
$this->display['userInfo'] = $usersRepository->findOneById($request->getSession()->get("userid"));
}
}
function addDisplay($key,$value){
$this->display[$key] = $value;
}
#[Route('/', name: 'app_site_mainpage', defaults: ['_locale'=>'ru'])]
public function index(String $_locale, MenuRepository $menuRepository,UsersCommentRepository $usersCommentRepository): Response
{
$usersComments = $usersCommentRepository->findBy(['status'=>0],['date'=>'desc'],10);
return $this->view('site/index.html.twig',[
'reviews'=>$usersComments
]);
}
#[Route('/{_locale}/site', name: 'app_site')]
public function indexSite(String $_locale): Response
{ return $this->redirect('/'.$_locale);
}
#[Route('/{_locale}/site/content/{url}', name: 'app_content', defaults: ['_locale'=>'ru'] )]
public function content(String $url,
ContentRepository $contentRepository,
MenuRepository $menuRepository): Response
{
return $this->view('site/content.html.twig', [
'content'=>$contentRepository->findOneBy(['url'=>$url])
]);
}
#[Route('/{_locale}/site/replies/{page}', name: 'app_content_replies', defaults: ['page'=>1,'_locale'=>'ru'] )]
public function replies(String $page,UsersCommentRepository $usersCommentRepository): Response
{
$perPage = 5;
$offset = $page*$perPage-$perPage;
return $this->view('site/replies.html.twig', [
'list'=>$usersCommentRepository->findBy(['status'=>0],['date'=>'desc',$perPage,$offset])
]);
}
function view($template,$params=[]){
foreach($this->display as $K =>$v){
$params[$K] = $v;
}
return $this->render($template,$params);
}
#[Route('/app/auth', name: 'auth')]
function auth(Request $request, UsersRepository $usersRepository){
if($request->get("login")){
$checkUser = $usersRepository->auth($request->get("login"),$request->get("password"));
if($checkUser['status'] == 1){
$request->getSession()->set("userid",$checkUser['userid']);
return $this->redirect("/user/profile");
}
$this->display['errorMessage'] = $checkUser['desc'];
}
return $this->render("user/auth.html.twig",$this->display);
}
#[Route('/register', name: 'register')]
function register(Request $request, UsersRepository $usersRepository){
if($request->get("login")){
$checkUser = $usersRepository->auth($request->get("login"),$request->get("password"));
if($checkUser['status'] == 1){
$request->getSession()->set("userid",$checkUser['userid']);
return $this->redirect("/user/");
}
}
return $this->render("user/auth.html.twig",$this->display);
}
#[Route('/app/exit', name: 'register')]
function exitApp(Request $request){
$request->getSession()->clear();
return $this->redirect('/');
}
}