Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2764f3c

Browse filesBrowse files
committed
feature #39893 [HttpKernel] Show full URI when route not found (ruudk)
This PR was merged into the 5.3-dev branch. Discussion ---------- [HttpKernel] Show full URI when route not found | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? |no | New feature? | yes | Deprecations? | no | License | MIT When accessing a route that does not exist, Symfony throws a `NotFoundHttpException` that says `No route found for "POST /path"`. On some projects this might be good enough to find the root cause, but on projects that have lots of routes on different hosts, it becomes hard to understand how the request was initiated. Was it done over HTTP or HTTPS? What was the hostname? Did the user specify a port? To make this easier, we now show the full URI of the path, like this: `No route found for "POST https://www.symfony.com/path"`. Commits ------- 6f5c9ab Show full URI when route not found
2 parents 1adfede + 6f5c9ab commit 2764f3c
Copy full SHA for 2764f3c

File tree

Expand file treeCollapse file tree

2 files changed

+59
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+59
-2
lines changed

‎src/Symfony/Component/HttpKernel/EventListener/RouterListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/EventListener/RouterListener.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ public function onKernelRequest(RequestEvent $event)
127127
unset($parameters['_route'], $parameters['_controller']);
128128
$request->attributes->set('_route_params', $parameters);
129129
} catch (ResourceNotFoundException $e) {
130-
$message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
130+
$message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getUriForPath($request->getPathInfo()));
131131

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

136136
throw new NotFoundHttpException($message, $e);
137137
} catch (MethodNotAllowedException $e) {
138-
$message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), implode(', ', $e->getAllowedMethods()));
138+
$message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getUriForPath($request->getPathInfo()), implode(', ', $e->getAllowedMethods()));
139139

140140
throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
141141
}

‎src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
use Symfony\Component\HttpKernel\EventListener\RouterListener;
2525
use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
2626
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
27+
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
28+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2729
use Symfony\Component\HttpKernel\HttpKernel;
2830
use Symfony\Component\HttpKernel\HttpKernelInterface;
31+
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
2932
use Symfony\Component\Routing\Exception\NoConfigurationException;
33+
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
3034
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
3135
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
3236
use Symfony\Component\Routing\RequestContext;
@@ -217,4 +221,57 @@ public function testRequestWithBadHost()
217221
$listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
218222
$listener->onKernelRequest($event);
219223
}
224+
225+
public function testResourceNotFoundException()
226+
{
227+
$this->expectException(NotFoundHttpException::class);
228+
$this->expectExceptionMessage('No route found for "GET https://www.symfony.com/path" (from "https://www.google.com")');
229+
230+
$context = new RequestContext();
231+
232+
$urlMatcher = $this->createMock(UrlMatcherInterface::class);
233+
234+
$urlMatcher->expects($this->any())
235+
->method('getContext')
236+
->willReturn($context);
237+
238+
$urlMatcher->expects($this->any())
239+
->method('match')
240+
->willThrowException(new ResourceNotFoundException());
241+
242+
$kernel = $this->createMock(HttpKernelInterface::class);
243+
$request = Request::create('https://www.symfony.com/path');
244+
$request->headers->set('referer', 'https://www.google.com');
245+
246+
$event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
247+
248+
$listener = new RouterListener($urlMatcher, $this->requestStack);
249+
$listener->onKernelRequest($event);
250+
}
251+
252+
public function testMethodNotAllowedException()
253+
{
254+
$this->expectException(MethodNotAllowedHttpException::class);
255+
$this->expectExceptionMessage('No route found for "GET https://www.symfony.com/path": Method Not Allowed (Allow: POST)');
256+
257+
$context = new RequestContext();
258+
259+
$urlMatcher = $this->createMock(UrlMatcherInterface::class);
260+
261+
$urlMatcher->expects($this->any())
262+
->method('getContext')
263+
->willReturn($context);
264+
265+
$urlMatcher->expects($this->any())
266+
->method('match')
267+
->willThrowException(new MethodNotAllowedException(['POST']));
268+
269+
$kernel = $this->createMock(HttpKernelInterface::class);
270+
$request = Request::create('https://www.symfony.com/path');
271+
272+
$event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
273+
274+
$listener = new RouterListener($urlMatcher, $this->requestStack);
275+
$listener->onKernelRequest($event);
276+
}
220277
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.