src/Controller/WhereToBuyController.php line 34

  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\Traits\HreflangsControllerTrait;
  4. use App\Controller\Traits\MetatagsControllerTrait;
  5. use App\Controller\Traits\StaticPageHelperTrait;
  6. use App\Entity\WhereToBuy;
  7. use App\Entity\WhereToBuyCustomMarket;
  8. use App\Repository\WhereToBuyCustomMarketRepository;
  9. use App\Repository\WhereToBuyRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class WhereToBuyController extends AbstractController
  17. {
  18.     use MetatagsControllerTraitHreflangsControllerTraitStaticPageHelperTrait;
  19.     private $em;
  20.     private $transalor;
  21.     public function __construct(EntityManagerInterface $emTranslatorInterface $translator){
  22.         $this->em $em;
  23.         $this->transalor $translator;
  24.     }
  25.     #[Route(path:[
  26.         'it' => '/trovali-qui']
  27.         , name'app_wheretobuy')]
  28.     public function indexWheretobuy(Request $request)
  29.     {
  30.         list ($metatags$hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request$this->em, ['it']);
  31.         $Wheretobuy $this->em->getRepository(WhereToBuy::class)->findBy([
  32.             'isEnabled' => true,
  33.         ]);
  34.         // dd($Wheretobuy);
  35.         return $this->render('wheretobuy/index.html.twig', [
  36.             'wheretobuy' => $Wheretobuy,
  37.             'metatags' => $metatags,
  38.             'hreflangs' => $hreflangs
  39.         ]);
  40.     }
  41.     #[Route(path:[
  42.         'it' => '/trovali-qui/api/v1']
  43.         , name'app_wheretobuy_api')]
  44.     public function apiWheretobuy(Request $request)
  45.     {
  46.         if($request->isMethod('POST')){
  47.             $province $request->get('province');
  48.             $type $request->get('type');
  49.             // dd($province, $type);
  50.             /** @var WhereToBuyCustomMarketRepository $whereToBuyCustomMarketRepository */
  51.             $whereToBuyCustomMarketRepository $this->em->getRepository(WhereToBuyCustomMarket::class);
  52.             $stores $whereToBuyCustomMarketRepository->findByRegionAndType($province$type);
  53.             $response['html'] = $this->renderView('wheretobuy/_wheretobuy_api.html.twig', [
  54.                 'stores' => $stores
  55.             ]);
  56.             return new JsonResponse($response200);
  57.             // dd($stores);
  58.             
  59.         } else {
  60.             return new JsonResponse('This is not a valid request'404);
  61.         }
  62.     }
  63.     
  64. }