Skip to content

Instantly share code, notes, and snippets.

@anvodev
Created May 3, 2020 11:05
Show Gist options
  • Select an option

  • Save anvodev/3fbe1bf8c9b1d72352bea9f796f02ec2 to your computer and use it in GitHub Desktop.

Select an option

Save anvodev/3fbe1bf8c9b1d72352bea9f796f02ec2 to your computer and use it in GitHub Desktop.
RouterListener::onKernelRequest
<?php
//vendor/symfony/http-kernel/EventListener/RouterListener.php
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
// add attributes based on the request (routing)
try {
$parameters = $this->matcher->matchRequest($request);
$request->attributes->add($parameters);
unset($parameters['_route'], $parameters['_controller']);
$request->attributes->set('_route_params', $parameters);
} catch (ResourceNotFoundException $e) {
$message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
throw new NotFoundHttpException($message, $e);
} catch (MethodNotAllowedException $e) {
$message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), implode(', ', $e->getAllowedMethods()));
throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment