src/Controller/DefaultController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use App\Model\DataObject\Product;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Knp\Component\Pager\PaginatorInterface;
  8. use Pimcore\Model\DataObject\Erich;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class DefaultController extends FrontendController
  11. {
  12.     /**
  13.      * @param Request $request
  14.      * @return Response
  15.      */
  16.     public function defaultAction(Request $requestPaginatorInterface $paginator): Response
  17.     {
  18.         $query $request->query->get('q''');
  19.         // Fetch products based on search query
  20.         $products = new Erich\Listing();
  21.         $products->setCondition("sku IS NOT NULL");
  22.         if ($query) {            
  23.             $products->addConditionParam("name_de LIKE ? OR sku LIKE ?",["%$query%""%$query%"]);
  24.             
  25.         }
  26.         // Paginate the results
  27.         $pagination $paginator->paginate(
  28.             $products,
  29.             $request->query->getInt('page'1), // page number
  30.             10 // limit per page
  31.         );
  32.         return $this->render('default/default.html.twig', [
  33.             'pagination' => $pagination,
  34.             'query' => $query,
  35.         ]);
  36.     }
  37.     /**
  38.      * @Route("/product/{id}", name="product_detail", methods={"GET"})
  39.      */
  40.     public function detailAction($id): Response
  41.     {
  42.         $product Product::getById($id);
  43.         if (!$product) {
  44.             throw $this->createNotFoundException('Product not found');
  45.         }
  46.         return $this->render('default/detail.html.twig', [
  47.             'product' => $product,
  48.         ]);
  49.     }
  50. }