src/Controller/CategoryController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\CategoryRepository;
  4. use Doctrine\ORM\PersistentCollection;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class CategoryController extends AbstractController
  10. {
  11. /**
  12. * @Route("/category/{slug}", name="category")
  13. */
  14. public function index(Request $request, CategoryRepository $categoryRepository): Response
  15. {
  16. $slug = $request->attributes->get('slug');
  17. /** @var PersistentCollection $products */
  18. $products = $categoryRepository->findOneBy(['slug' => $slug])->getProducts()->toArray();
  19. return $this->render('category/index.html.twig', [
  20. 'controller_name' => 'CategoryController',
  21. 'slides' => array_chunk($categoryRepository->findAll(), 4),
  22. 'products' => $products,
  23. ]);
  24. }
  25. }