Skip to content
Snippets Groups Projects
Commit 29824039 authored by Larry Garfield's avatar Larry Garfield
Browse files

Convert the matcher into a RouterListener, which in turn handles the error...

Convert the matcher into a RouterListener, which in turn handles the error handling to return the right kind of exception at the right time on a 404.
parent 16dbf2ec
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -13,6 +13,7 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Drupal\Core\EventSubscriber\HtmlSubscriber;
......@@ -35,9 +36,7 @@ function execute(Request $request) {
$dispatcher = $this->getDispatcher();
$matcher = $this->getMatcher($request);
// Push path paramaters into attributes.
$request->attributes->add($matcher->match($request->getPathInfo()));
$dispatcher->addSubscriber(new RouterListener($matcher));
$resolver = $this->getControllerResolver($request);
......@@ -51,7 +50,7 @@ function execute(Request $request) {
$response = $error_event->getResponse();
}
else {
$response = new Response('An error occurred', 500);
$response = new Response('An error occurred in the wrong place', 500);
}
}
......@@ -79,12 +78,8 @@ protected function getMatcher(Request $request) {
return $matcher;
}
protected function getControllerResolver($request) {
// Get the controller(page callback) from the resolver.
protected function getControllerResolver() {
$resolver = new ControllerResolver();
$controller = $resolver->getController($request);
$arguments = $resolver->getArguments($request, $controller);
return $resolver;
}
......
......@@ -8,6 +8,8 @@
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @file
......@@ -24,12 +26,18 @@ protected function isHtmlRequestEvent(GetResponseEvent $event) {
return in_array('text/html', $event->getRequest()->getAcceptableContentTypes());
}
public function onResourceNotFoundException(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof ResourceNotFoundException) {
public function onNotFoundHttpException(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof NotFoundHttpException) {
$event->setResponse(new Response('Not Found', 404));
}
}
public function onMethodAllowedException(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof MethodNotAllowedException) {
$event->setResponse(new Response('Method Not Allowed', 405));
}
}
public function onView(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event)) {
$page_callback_result = $event->getControllerResult();
......@@ -42,8 +50,9 @@ public function onAccessDeniedException(Event $event) {
}
static function getSubscribedEvents() {
$events[KernelEvents::EXCEPTION][] = array('onResourceNotFoundException');
$events[KernelEvents::EXCEPTION][] = array('onNotFoundHttpException');
$events[KernelEvents::EXCEPTION][] = array('onAccessDeniedException');
$events[KernelEvents::EXCEPTION][] = array('onMethodAllowedException');
$events[KernelEvents::VIEW][] = array('onView');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment