Refactor HomeController in PostController
This commit is contained in:
parent
67a4f95882
commit
e399f79d29
2 changed files with 17 additions and 28 deletions
|
@ -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
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
Reference in a new issue