src/EventSubscriber/Routing/CityPrefixSubscriber.php line 18

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-07-18
  5.  * Time: 17:05
  6.  */
  7. namespace App\EventSubscriber\Routing;
  8. use App\Event\Routing\FilterGeneratedUrlEvent;
  9. use App\Event\Routing\RoutingEvents;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class CityPrefixSubscriber implements EventSubscriberInterface
  12. {
  13.     const CITY_PARAMETER 'city';
  14.     public function ignoreCityParameter(FilterGeneratedUrlEvent $event): void
  15.     {
  16.         $url $event->getUrl();
  17.         $routeParameters $event->getParameters();
  18.         if (isset($routeParameters[self::CITY_PARAMETER]) && false !== $pos strpos($url'?')) {
  19.             $qs substr($url$pos 1);
  20.             parse_str($qs$parameters);
  21.             unset($parameters[self::CITY_PARAMETER]);
  22.             if (!empty($parameters)) {
  23.                 $url str_replace("?$qs"'?'.http_build_query($parameters), $url);
  24.             } else {
  25.                 $url str_replace("?$qs"''$url);
  26.             }
  27.             $event->setUrl($url);
  28.         }
  29.     }
  30.     /**
  31.      * @inheritDoc
  32.      */
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             RoutingEvents::POST_GENERATE => 'ignoreCityParameter',
  37.         ];
  38.     }
  39. }