4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\EventListener;
14 use Psr\Log\LoggerInterface;
15 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16 use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
17 use Symfony\Component\HttpKernel\KernelEvents;
18 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
19 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20 use Symfony\Component\HttpFoundation\RequestStack;
21 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
22 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
23 use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
24 use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
25 use Symfony\Component\Routing\RequestContext;
26 use Symfony\Component\Routing\RequestContextAwareInterface;
27 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
28 use Symfony\Component\HttpFoundation\Request;
31 * Initializes the context from the request and sets request attributes based on a matching route.
33 * @author Fabien Potencier <fabien@symfony.com>
35 class RouterListener implements EventSubscriberInterface
40 private $requestStack;
45 * @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
46 * @param RequestStack $requestStack A RequestStack instance
47 * @param RequestContext|null $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
48 * @param LoggerInterface|null $logger The logger
50 * @throws \InvalidArgumentException
52 public function __construct($matcher, RequestStack $requestStack, RequestContext $context = null, LoggerInterface $logger = null)
54 if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
55 throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
58 if (null === $context && !$matcher instanceof RequestContextAwareInterface) {
59 throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');
62 $this->matcher = $matcher;
63 $this->context = $context ?: $matcher->getContext();
64 $this->requestStack = $requestStack;
65 $this->logger = $logger;
68 private function setCurrentRequest(Request $request = null)
70 if (null !== $request) {
71 $this->context->fromRequest($request);
76 * After a sub-request is done, we need to reset the routing context to the parent request so that the URL generator
77 * operates on the correct context again.
79 * @param FinishRequestEvent $event
81 public function onKernelFinishRequest(FinishRequestEvent $event)
83 $this->setCurrentRequest($this->requestStack->getParentRequest());
86 public function onKernelRequest(GetResponseEvent $event)
88 $request = $event->getRequest();
90 $this->setCurrentRequest($request);
92 if ($request->attributes->has('_controller')) {
93 // routing is already done
97 // add attributes based on the request (routing)
99 // matching a request is more powerful than matching a URL path + context, so try that first
100 if ($this->matcher instanceof RequestMatcherInterface) {
101 $parameters = $this->matcher->matchRequest($request);
103 $parameters = $this->matcher->match($request->getPathInfo());
106 if (null !== $this->logger) {
107 $this->logger->info('Matched route "{route}".', array(
108 'route' => isset($parameters['_route']) ? $parameters['_route'] : 'n/a',
109 'route_parameters' => $parameters,
110 'request_uri' => $request->getUri(),
111 'method' => $request->getMethod(),
115 $request->attributes->add($parameters);
116 unset($parameters['_route'], $parameters['_controller']);
117 $request->attributes->set('_route_params', $parameters);
118 } catch (ResourceNotFoundException $e) {
119 $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
121 if ($referer = $request->headers->get('referer')) {
122 $message .= sprintf(' (from "%s")', $referer);
125 throw new NotFoundHttpException($message, $e);
126 } catch (MethodNotAllowedException $e) {
127 $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), implode(', ', $e->getAllowedMethods()));
129 throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
133 public static function getSubscribedEvents()
136 KernelEvents::REQUEST => array(array('onKernelRequest', 32)),
137 KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),