Skip to content

Commit f7ec212

Browse files
committed
Show welcome message if no routing configuration could be found
1 parent 23e7623 commit f7ec212

File tree

9 files changed

+89
-0
lines changed

9 files changed

+89
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
3.4.0
55
-----
66

7+
* Added `NoConfigurationException`.
78
* Added the possibility to define a prefix for all routes of a controller via @Route(name="prefix_")
89
* Added support for prioritized routing loaders.
910
* Add matched and default parameters to redirect responses
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Exception;
13+
14+
/**
15+
* Exception thrown when no routes are configured.
16+
*
17+
* @author Yonel Ceruto <[email protected]>
18+
*/
19+
class NoConfigurationException extends ResourceNotFoundException
20+
{
21+
}

Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
155155
}
156156
}
157157

158+
if ('' === $code) {
159+
$code .= " if ('/' === \$pathinfo) {\n";
160+
$code .= " throw new Symfony\Component\Routing\Exception\NoConfigurationException();\n";
161+
$code .= " }\n";
162+
}
163+
158164
return $code;
159165
}
160166

Matcher/RequestMatcherInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Routing\Matcher;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Routing\Exception\NoConfigurationException;
1516
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
1617
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
1718

@@ -32,6 +33,7 @@ interface RequestMatcherInterface
3233
*
3334
* @return array An array of parameters
3435
*
36+
* @throws NoConfigurationException If no routing configuration could be found
3537
* @throws ResourceNotFoundException If no matching resource could be found
3638
* @throws MethodNotAllowedException If a matching resource was found but the request method is not allowed
3739
*/

Matcher/UrlMatcher.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Routing\Matcher;
1313

1414
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
15+
use Symfony\Component\Routing\Exception\NoConfigurationException;
1516
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
1617
use Symfony\Component\Routing\RouteCollection;
1718
use Symfony\Component\Routing\RequestContext;
@@ -91,6 +92,10 @@ public function match($pathinfo)
9192
return $ret;
9293
}
9394

95+
if (0 === count($this->routes) && '/' === $pathinfo) {
96+
throw new NoConfigurationException();
97+
}
98+
9499
throw 0 < count($this->allow)
95100
? new MethodNotAllowedException(array_unique($this->allow))
96101
: new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
@@ -123,6 +128,7 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
123128
*
124129
* @return array An array of parameters
125130
*
131+
* @throws NoConfigurationException If no routing configuration could be found
126132
* @throws ResourceNotFoundException If the resource could not be found
127133
* @throws MethodNotAllowedException If the resource was found but the request method is not allowed
128134
*/

Matcher/UrlMatcherInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Routing\Matcher;
1313

14+
use Symfony\Component\Routing\Exception\NoConfigurationException;
1415
use Symfony\Component\Routing\RequestContextAwareInterface;
1516
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
1617
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
@@ -32,6 +33,7 @@ interface UrlMatcherInterface extends RequestContextAwareInterface
3233
*
3334
* @return array An array of parameters
3435
*
36+
* @throws NoConfigurationException If no routing configuration could be found
3537
* @throws ResourceNotFoundException If the resource could not be found
3638
* @throws MethodNotAllowedException If the resource was found but the request method is not allowed
3739
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
4+
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
5+
use Symfony\Component\Routing\RequestContext;
6+
7+
/**
8+
* This class has been auto-generated
9+
* by the Symfony Routing Component.
10+
*/
11+
class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
12+
{
13+
public function __construct(RequestContext $context)
14+
{
15+
$this->context = $context;
16+
}
17+
18+
public function match($pathinfo)
19+
{
20+
$allow = array();
21+
$pathinfo = rawurldecode($pathinfo);
22+
$trimmedPathinfo = rtrim($pathinfo, '/');
23+
$context = $this->context;
24+
$request = $this->request;
25+
$requestMethod = $canonicalMethod = $context->getMethod();
26+
$scheme = $context->getScheme();
27+
28+
if ('HEAD' === $requestMethod) {
29+
$canonicalMethod = 'GET';
30+
}
31+
32+
33+
if ('/' === $pathinfo) {
34+
throw new Symfony\Component\Routing\Exception\NoConfigurationException();
35+
}
36+
37+
throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
38+
}
39+
}

Tests/Matcher/Dumper/PhpMatcherDumperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ public function getRouteCollections()
374374
$trailingSlashCollection->add('regex_not_trailing_slash_POST_method', new Route('/not-trailing/regex/post-method/{param}', array(), array(), array(), '', array(), array('POST')));
375375

376376
return array(
377+
array(new RouteCollection(), 'url_matcher0.php', array()),
377378
array($collection, 'url_matcher1.php', array()),
378379
array($redirectCollection, 'url_matcher2.php', array('base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher')),
379380
array($rootprefixCollection, 'url_matcher3.php', array()),

Tests/Matcher/UrlMatcherTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,15 @@ public function testHostIsCaseInsensitive()
427427
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
428428
$this->assertEquals(array('_route' => 'foo', 'locale' => 'en'), $matcher->match('/'));
429429
}
430+
431+
/**
432+
* @expectedException \Symfony\Component\Routing\Exception\NoConfigurationException
433+
*/
434+
public function testNoConfiguration()
435+
{
436+
$coll = new RouteCollection();
437+
438+
$matcher = new UrlMatcher($coll, new RequestContext());
439+
$matcher->match('/');
440+
}
430441
}

0 commit comments

Comments
 (0)