src/Controller/PostController.php line 48

  1. <?php
  2. namespace Cms\Controller;
  3. use Cms\Entity\Post;
  4. use Cms\Repository\PostRepository;
  5. use Symfony\Bridge\Doctrine\Attribute\MapEntity;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\DependencyInjection\Attribute\Autowire;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. #[Route('/post')]
  12. class PostController extends AbstractController
  13. {
  14.     private int $postPerPage;
  15.     public function __construct(
  16.         #[Autowire('%cms.post_per_page.public%')]
  17.         int $postPerPage
  18.     ) {
  19.         $this->postPerPage $postPerPage;
  20.     }
  21.     #[Route('/{type<tags|category>}/{slug<[a-z-]+>}'name'cms_post_index'defaults: ['type' => '''slug' => ''], methods: ['GET'])]
  22.     public function index(Request $requeststring $typestring $slugPostRepository $postRepository): Response
  23.     {
  24.         $criteria = ['deleted' => false'published' => true];
  25.         if ($type !== '') {
  26.             $criteria[$type] = $slug;
  27.         }
  28.         $posts $postRepository->getPaginated($this->postPerPage$criteria);
  29.         if ($request->headers->get('Turbo-Frame') === 'frame_content') {
  30.             return $this->render('post/index.frame.html.twig', [
  31.                 'posts' => $posts,
  32.             ]);
  33.         }
  34.         return $this->render('post/index.html.twig', [
  35.             'posts' => $posts,
  36.         ]);
  37.     }
  38.     #[Route('/{slug}'name'cms_post_show'requirements: ['slug' => '[a-z0-9-]+'], methods: ['GET'])]
  39.     public function show(Request $request, #[MapEntity(mapping: ['slug' => 'slug'])] Post $post): Response
  40.     {
  41.         if ($request->headers->get('Turbo-Frame') === 'frame_content') {
  42.             return $this->render('post/show.frame.html.twig', [
  43.                 'post' => $post,
  44.             ]);
  45.         }
  46.         return $this->render('post/show.html.twig', [
  47.             'post' => $post,
  48.         ]);
  49.     }
  50. }