src/Controller/Mvc/BlogController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Mvc;
  3. use App\Entity\Blog\Entry;
  4. use App\Form\Blog\ListType;
  5. use App\Repository\Blog\CategoryRepository;
  6. use App\Repository\Blog\EntryRepository;
  7. use App\Service\CmsHelperService;
  8. use App\Service\FileUploader\UploadService;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route("/blog")
  16.  */
  17. class BlogController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/", name="blog_index", methods={"GET"})
  21.      */
  22.     public function index(
  23.         Request $request,
  24.         EntryRepository $repository,
  25.         CategoryRepository $categoryRepository,
  26.         CmsHelperService $helper
  27.     ): Response {
  28.         $form $this->createForm(ListType::class);
  29.         $criteria $request->query->all();
  30.         $criteria['limit'] = $criteria['limit'] ?? 8;
  31.         $form->submit($criteria);
  32.         $paginate $helper->listHandle($repository$form->getViewData());
  33.         return $this->render("pages/blog/index.html.twig", [
  34.             "paginate" => $paginate,
  35.             "data" => $paginate->getPaginator()->getIterator(),
  36.             "categories" => $categoryRepository->getCategoriesHaveEntry(),
  37.             "current_category" => $form->getViewData()['category']
  38.         ]);
  39.     }
  40.     /**
  41.      * @Route("/{id}", name="blog_detail", methods={"GET"}, requirements={"id" = "\d+"})
  42.      */
  43.     public function detail(
  44.         EntryRepository $repository,
  45.         $id
  46.     ) {
  47.         $Entry $repository->getEntry($id);
  48.         if(!$Entry) {
  49.             throw new NotFoundHttpException('page not found');
  50.         }
  51.         if($Entry->getLinkUrl()) {
  52.             return $this->redirect($Entry->getLinkUrl());
  53.         }
  54.         return $this->render("pages/blog/detail.html.twig", [
  55.             "entry" => $Entry
  56.         ]);
  57.     }
  58.     /**
  59.      * @Route("/image/{filename}", name="blog_image", methods={"GET"})
  60.      */
  61.     public function image(UploadService $uploader$filename) {
  62.         $config $uploader->getUploadConfig(new Entry"main_img");
  63.         return $uploader->output($filename$config);
  64.     }
  65.     /**
  66.      * @Route("/headline", name="blog_headline", methods={"GET"})
  67.      */
  68.     public function headline(Request $requestEntryRepository $repository) {
  69.         $criteria $request->query->all();
  70.         $qb $repository->getListQuery($criteria);
  71.         $qb->setMaxResults(isset($criteria['limit']) && $criteria['limit']? $criteria['limit']: 6);
  72.         return $this->render('pages/blog/headline.html.twig', [
  73.             "data" => $qb->getQuery()->getResult()
  74.         ]);
  75.     }
  76.   }