8000 [HttpKernel] Show full URI when route not found by ruudk · Pull Request #39893 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ public function onKernelRequest(RequestEvent $event)
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());
$message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getUriForPath($request->getPathInfo()));

if ($referer = $request->headers->get('referer')) {
$message .= sprintf(' (from "%s")', $referer);
}

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()));
$message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getUriForPath($request->getPathInfo()), implode(', ', $e->getAllowedMethods()));

throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\NoConfigurationException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\RequestContext;
Expand Down Expand Up @@ -217,4 +221,57 @@ public function testRequestWithBadHost()
$listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
$listener->onKernelRequest($event);
}

public function testResourceNotFoundException()
{
$this->expectException(NotFoundHttpException::class);
$this->expectExceptionMessage('No route found for "GET https://www.symfony.com/path" (from "https://www.google.com")');

$context = new RequestContext();

$urlMatcher = $this->createMock(UrlMatcherInterface::class);

$urlMatcher->expects($this->any())
->method('getContext')
->willReturn($context);

$urlMatcher->expects($this->any())
->method('match')
->willThrowException(new ResourceNotFoundException());

$kernel = $this->createMock(HttpKernelInterface::class);
$request = Request::create('https://www.symfony.com/path');
$request->headers->set('referer', 'https://www.google.com');

$event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);

$listener = new RouterListener($urlMatcher, $this->requestStack);
$listener->onKernelRequest($event);
}

public function testMethodNotAllowedException()
{
$this->expectException(MethodNotAllowedHttpException::class);
$this->expectExceptionMessage('No route found for "GET https://www.symfony.com/path": Method Not Allowed (Allow: POST)');

$context = new RequestContext();

$urlMatcher = $this->createMock(UrlMatcherInterface::class);

$urlMatcher->expects($this->any())
->method('getContext')
->willReturn($context);

$urlMatcher->expects($this->any())
->method('match')
->willThrowException(new MethodNotAllowedException(['POST']));

$kernel = $this->createMock(HttpKernelInterface::class);
$request = Request::create('https://www.symfony.com/path');

$event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);

$listener = new RouterListener($urlMatcher, $this->requestStack);
$listener->onKernelRequest($event);
}
}
0