From a7b2b7e92b5ce3e234464d47ee3b5cbfa41a55ed Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 6 Feb 2013 22:20:41 +0100 Subject: [PATCH] fixed Request management for RequestListener --- .../Resources/config/routing.xml | 1 + .../EventListener/RouterListener.php | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml index 6d6671b7ae33..9e21db451915 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml @@ -94,6 +94,7 @@ + diff --git a/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php b/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php index 606b358e72f0..58ea6f0a5436 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php @@ -23,6 +23,7 @@ use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContextAwareInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpFoundation\Request; /** * Initializes the context from the request and sets request attributes based on a matching route. @@ -59,12 +60,31 @@ public function __construct($matcher, RequestContext $context = null, LoggerInte $this->logger = $logger; } + /** + * Sets the current Request. + * + * The application should call this method whenever the Request + * object changes (entering a Request scope for instance, but + * also when leaving a Request scope -- especially when they are + * nested). + * + * @param Request|null $request A Request instance + */ + public function setRequest(Request $request = null) + { + if (null !== $request) { + $this->context->fromRequest($request); + } + } + public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); // initialize the context that is also used by the generator (assuming matcher and generator share the same context instance) - $this->context->fromRequest($request); + // we call setRequest even if most of the time, it has already been done to keep compatibility + // with frameworks which do not use the Symfony service container + $this->setRequest($request); if ($request->attributes->has('_controller')) { // routing is already done