diff --git a/EventListener/RequestListener.php b/EventListener/RequestListener.php index 9c82869..300ab89 100644 --- a/EventListener/RequestListener.php +++ b/EventListener/RequestListener.php @@ -14,6 +14,8 @@ use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\Routing\Route; /** * Request listener @@ -67,6 +69,10 @@ public function __construct(Container $serviceContainer, array $redirectConf, $ */ public function handleRequest(GetResponseEvent $event) { + // only handle master request, do not handle sub request like esi includes + if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) { + return; + } // Sets the flag for the response handled by the GET switch param and the type of the view. if ($this->deviceView->hasSwitchParam()) { @@ -321,6 +327,7 @@ private function getRedirectUrl($platform) */ private function getRoutingOption($name) { + $option = null; $route = $this ->container ->get('router') @@ -328,7 +335,9 @@ private function getRoutingOption($name) ->get($this->container->get('request')->get('_route')) ; - $option = $route->getOption($name); + if ($route instanceof Route) { + $option = $route->getOption($name); + } if (!$option) { $option = $this->redirectConf[$name]['action']; @@ -338,7 +347,7 @@ private function getRoutingOption($name) return $option; } - return false; + return null; } /** diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 9f142b6..d8a820b 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -17,7 +17,8 @@ services: class: %mobile_detect.request_listener.class% arguments: [ '@service_container', %mobile_detect.redirect%, %mobile_detect.switch_device_view.save_referer_path% ] tags: - - { name: kernel.event_listener, event: kernel.request, method: handleRequest } + # must be registered after the Router to have access to the _route, and after the Symfony LocaleListener + - { name: kernel.event_listener, event: kernel.request, method: handleRequest, priority: 1 } - { name: kernel.event_listener, event: kernel.response, method: handleResponse } mobile_detect.twig.extension: class: %mobile_detect.twig.extension.class%