src/Controller/WhereToBuyController.php line 34
<?php
namespace App\Controller;
use App\Controller\Traits\HreflangsControllerTrait;
use App\Controller\Traits\MetatagsControllerTrait;
use App\Controller\Traits\StaticPageHelperTrait;
use App\Entity\WhereToBuy;
use App\Entity\WhereToBuyCustomMarket;
use App\Repository\WhereToBuyCustomMarketRepository;
use App\Repository\WhereToBuyRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class WhereToBuyController extends AbstractController
{
use MetatagsControllerTrait, HreflangsControllerTrait, StaticPageHelperTrait;
private $em;
private $transalor;
public function __construct(EntityManagerInterface $em, TranslatorInterface $translator){
$this->em = $em;
$this->transalor = $translator;
}
#[Route(path:[
'it' => '/trovali-qui']
, name: 'app_wheretobuy')]
public function indexWheretobuy(Request $request)
{
list ($metatags, $hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request, $this->em, ['it']);
$Wheretobuy = $this->em->getRepository(WhereToBuy::class)->findBy([
'isEnabled' => true,
]);
// dd($Wheretobuy);
return $this->render('wheretobuy/index.html.twig', [
'wheretobuy' => $Wheretobuy,
'metatags' => $metatags,
'hreflangs' => $hreflangs
]);
}
#[Route(path:[
'it' => '/trovali-qui/api/v1']
, name: 'app_wheretobuy_api')]
public function apiWheretobuy(Request $request)
{
if($request->isMethod('POST')){
$province = $request->get('province');
$type = $request->get('type');
// dd($province, $type);
/** @var WhereToBuyCustomMarketRepository $whereToBuyCustomMarketRepository */
$whereToBuyCustomMarketRepository = $this->em->getRepository(WhereToBuyCustomMarket::class);
$stores = $whereToBuyCustomMarketRepository->findByRegionAndType($province, $type);
$response['html'] = $this->renderView('wheretobuy/_wheretobuy_api.html.twig', [
'stores' => $stores
]);
return new JsonResponse($response, 200);
// dd($stores);
} else {
return new JsonResponse('This is not a valid request', 404);
}
}
}