src/Controller/CitySuggestController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class CitySuggestController extends AbstractController
  8. {
  9.     /**
  10.      * @Route("/city_suggest", name="city_suggest")
  11.      */
  12.     public function index()
  13.     {
  14.         //@todo: make caching here
  15.         $request Request::createFromGlobals();
  16.         $limit $request->query->get('limit');
  17.         if(empty($limit)) $limit 10;
  18.         $query $request->query->get('q');
  19.         $avtd_query 'https://api.avtodispetcher.ru/v1/cities?limit=' $limit '&q=' urlencode($query);
  20.         if(mb_strlen($query) > 2)
  21.             $res '{"data":' file_get_contents($avtd_query) . '}';
  22.         else
  23.             $res json_encode([]);
  24.         $response = new Response();
  25.         //$response->setContent(json_encode($array_of_suggestions));
  26.         $response->setContent($res);
  27.         $response->headers->set('Content-Type''application/json');
  28.         $response->headers->set('Access-Control-Allow-Origin''https://moto.tours');
  29.         return $response;
  30.     }
  31. }