1
0
Fork 0

Refactor HomeController in PostController

This commit is contained in:
Ethanell 2021-01-16 19:36:56 +01:00
parent 67a4f95882
commit e399f79d29
2 changed files with 17 additions and 28 deletions

View file

@ -1,27 +0,0 @@
<?php
namespace App\Controller;
use App\Entity\Post;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
/**
* @Route("/{page}", name="home")
* @param int $page
* @return Response
*/
public function index(int $page = 0): Response
{
$repo = $this->getDoctrine()->getRepository(Post::class);
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
'posts' => $repo->getPublished($page*5, 5),
'pages' => round(count($repo->getPublished())/5, 0, PHP_ROUND_HALF_UP)-1,
'page' => $page
]);
}
}

View file

@ -16,13 +16,29 @@ use Symfony\Component\String\Slugger\AsciiSlugger;
class PostController extends AbstractController
{
/**
* @Route("/{page}", name="home")
* @param int $page
* @return Response
*/
public function index(int $page = 0): Response
{
$repo = $this->getDoctrine()->getRepository(Post::class);
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
'posts' => $repo->getPublished($page*5, 5),
'pages' => round(count($repo->getPublished())/5, 0, PHP_ROUND_HALF_UP)-1,
'page' => $page
]);
}
/**
* @Route("/post/{slug}", name="post")
* @param Request $request
* @param string $slug
* @return Response
*/
public function index(Request $request, string $slug): Response
public function post(Request $request, string $slug): Response
{
$post = $this->getDoctrine()->getRepository(Post::class)->findOneBy(array('slug' => $slug));
$form = $this->commentFormGenerator($post);