src/Controller/CategoryController.php line 126

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Category;
  4. use App\Form\CategoryType;
  5. use App\Repository\CategoryRepository;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  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. use App\Entity\Post;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. /**
  15.  * @Route("/category")
  16.  */
  17. class CategoryController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/", name="category_index", methods={"GET"})
  21.      *@IsGranted("ROLE_USER", message="No access! Get out!")
  22.      */
  23.     public function index(CategoryRepository $categoryRepository): Response
  24.     {
  25.         return $this->render('category/index.html.twig', [
  26.             'categories' => $categoryRepository->findAll(),
  27.         ]);
  28.     }
  29.     /**
  30.      * @Route("/new", name="category_new", methods={"GET","POST"})
  31.      */
  32.     public function new(Request $request): Response
  33.     {
  34.         $category = new Category();
  35.         $form $this->createForm(CategoryType::class, $category);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             $entityManager $this->getDoctrine()->getManager();
  39.             $category->setSlug($category->getName().'-'.uniqid());
  40.             $entityManager->persist($category);
  41.             $entityManager->flush();
  42.             $this->addFlash(
  43.                 'success',
  44.                 "Votre enregistrement  a été effectué  avec succès!"
  45.             );
  46.             return $this->redirectToRoute('category_index');
  47.         }
  48.         return $this->render('category/new.html.twig', [
  49.             'category' => $category,
  50.             'form' => $form->createView(),
  51.         ]);
  52.     }
  53.     /**
  54.      * @Route("/{slug}", name="category_show", methods={"GET"})
  55.      */
  56.     public function show(Category $category): Response
  57.     {
  58.         return $this->render('category/show.html.twig', [
  59.             'category' => $category,
  60.         ]);
  61.     }
  62.     /**
  63.      * @Route("/{slug}/edit", name="category_edit", methods={"GET","POST"})
  64.      */
  65.     public function edit(Request $requestCategory $category): Response
  66.     {
  67.         $form $this->createForm(CategoryType::class, $category);
  68.         $form->handleRequest($request);
  69.         if ($form->isSubmitted() && $form->isValid()) {
  70.             $category->setSlug($category->getName().'-'.uniqid());
  71.             $this->getDoctrine()->getManager()->flush();
  72.             return $this->redirectToRoute('category_index');
  73.         }
  74.         return $this->render('category/edit.html.twig', [
  75.             'category' => $category,
  76.             'form' => $form->createView(),
  77.         ]);
  78.     }
  79.     /**
  80.      * @Route("/{slug}", name="category_delete", methods={"DELETE"})
  81.      */
  82.     public function delete(Request $requestCategory $category): Response
  83.     {
  84.         if ($this->isCsrfTokenValid('delete'.$category->getSlug(), $request->request->get('_token'))) {
  85.             $entityManager $this->getDoctrine()->getManager();
  86.             $entityManager->remove($category);
  87.             $entityManager->flush();
  88.         }
  89.         return $this->redirectToRoute('category_index');
  90.     }
  91.     /**
  92.      * @Route("/blog/{category_name}", name="blog_category", methods={"GET"})
  93.      * @Entity("Category", expr="repository.find(category.name)")
  94.      */
  95.     public function blog(String $category_name): Response
  96.     {
  97.         $repository $this->getDoctrine()->getRepository(Post::class);
  98.         $posts $repository->findPostByCategorie($category_name);
  99.         $latest$repository->findBylast();
  100.         return $this->render('front/blog.html.twig', [
  101.             'category' => $category_name,
  102.             'posts' => $posts,
  103.             'latest' => $latest,
  104.         ]);
  105.     }
  106.     /**
  107.      * @Route("/newblog/{category_name}", name="new_blog_category", methods={"GET"})
  108.      * @Entity("Category", expr="repository.find(category.name)")
  109.      */
  110.     public function newblog(String $category_nameRequest $request): Response
  111.     {
  112.         $page =$request->query->getInt('page',1);
  113.         $repository $this->getDoctrine()->getRepository(Post::class);
  114.         $posts $repository->findPostByCategorie($category_name$page);
  115.         $latest$repository->findBylast();
  116.         //dd($posts);
  117.         return $this->render('newwebfront/blog.html.twig', [
  118.             'category' => $category_name,
  119.             'posts' => $posts,
  120.             'latest' => $latest,
  121.         ]);
  122.     }
  123. }