src/Controller/Posts/PostsController.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Posts;
  3. use App\Entity\Posts\Categorieposts;
  4. use App\Entity\Posts\Posts;
  5. use App\Repository\Posts\PostsRepository;
  6. use Knp\Component\Pager\PaginatorInterface;
  7. use MercurySeries\FlashyBundle\FlashyNotifier;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * @Route("/sejourner")
  14.  */
  15. class PostsController extends AbstractController
  16. {
  17.     /**
  18.      * @var FlashyNotifier
  19.      */
  20.     private $flashy;
  21.     /**
  22.      * @var PostsRepository
  23.      */
  24.     private $postsRepository;
  25.     public function __construct(PostsRepository $postsRepositoryFlashyNotifier $flashy)
  26.     {
  27.         $this->postsRepository $postsRepository;
  28.         $this->flashy $flashy;
  29.     }
  30.     /**
  31.      * @Route("/{slug}/{id}", name="posts.index")
  32.      * @param Request $request
  33.      * @param Categorieposts $categorieposts
  34.      * @param PaginatorInterface $paginator
  35.      * @return Response
  36.      */
  37.     public function index(Request $requestCategorieposts $categoriepostsPaginatorInterface $paginator): Response
  38.     {
  39.         $posts $paginator->paginate(
  40.             $this->getDoctrine()->getRepository(Posts::class)->findBy(['categories' => $categorieposts'online' => true], ['id' => 'DESC']), /* query NOT result */
  41.             $request->query->getInt('page'1)/*page number*/,
  42.             $request->query->getInt('limit'12)/*limit per page*/
  43.         );
  44.         return $this->render('pages/internes/posts/index.html.twig', [
  45.             'title' => $categorieposts->getName(),
  46.             'categorie' => $categorieposts,
  47.             'posts' => $posts,
  48.             'menu' => "Séjourner",
  49.             'current_page' => 'sejourner'
  50.         ]);
  51.     }
  52.     /**
  53.      * @Route("/details/{slug}/{id}", name="posts.show")
  54.      * @param $slug
  55.      * @param Posts $post
  56.      * @return Response
  57.      */
  58.     public function show($slugPosts $post): Response
  59.     {
  60.         if ($post->getSlug() !== $slug) {
  61.             return $this->redirectToRoute('posts.show', [
  62.                 'id' => $post->getId(),
  63.                 'slug' => $post->getSlug(),
  64.             ], 301);
  65.         }
  66.         $othersPost $this->postsRepository->getPostRandom($post$post->getCategories(), 3);
  67.         return $this->render('pages/internes/posts/show.html.twig', [
  68.             'title' => $post->getName(),
  69.             'post' => $post,
  70.             'othersPosts' => $othersPost,
  71.             'menu' => "Séjourner",
  72.             'current_page' => 'sejourner'
  73.         ]);
  74.     }
  75. }