Skip to content

Commit 82796ec

Browse files
committed
Merge pull request #253 from JoostK/global-wheres
Allow for setting of parameter patterns globally on the router
2 parents 67c5652 + 7879ec9 commit 82796ec

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/Illuminate/Routing/Router.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ class Router {
6666
*/
6767
protected $inspector;
6868

69+
/**
70+
* The global parameter patterns.
71+
*
72+
* @var array
73+
*/
74+
protected $patterns = array();
75+
6976
/**
7077
* The registered route binders.
7178
*
@@ -543,6 +550,8 @@ protected function createRoute($method, $pattern, $action)
543550

544551
))->setRouter($this);
545552

553+
$route->addRequirements($this->patterns);
554+
546555
$route->setRequirement('_method', $method);
547556

548557
// Once we have created the route, we will add them to our route collection
@@ -1037,6 +1046,18 @@ protected function callGlobalFilter(Request $request, $name, array $parameters =
10371046
}
10381047
}
10391048

1049+
/**
1050+
* Set a global where pattern on all routes
1051+
*
1052+
* @param string $key
1053+
* @param string $pattern
1054+
* @return void
1055+
*/
1056+
public function pattern($key, $pattern)
1057+
{
1058+
$this->patterns[$key] = $pattern;
1059+
}
1060+
10401061
/**
10411062
* Register a model binder for a wildcard.
10421063
*

tests/Routing/RoutingTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,28 @@ public function testFiltersCanBeDisabled()
350350
}
351351

352352

353+
/**
354+
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
355+
*/
353356
public function testWhereMethodForcesRegularExpressionMatch()
354357
{
355358
$router = new Router;
356359
$router->get('/foo/{name}/{age}', function($name, $age) { return $name.$age; })->where('age', '[0-9]+');
357-
$request = Request::create('/foo/taylor/25', 'GET');
358-
$this->assertEquals('taylor25', $router->dispatch($request)->getContent());
360+
$request = Request::create('/foo/taylor/abc', 'GET');
361+
$this->assertEquals('taylorabc', $router->dispatch($request)->getContent());
362+
}
363+
364+
365+
/**
366+
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
367+
*/
368+
public function testGlobalParameterPatternsAreApplied()
369+
{
370+
$router = new Router;
371+
$router->pattern('age', '[0-9]+');
372+
$router->get('/foo/{name}/{age}', function($name, $age) { return $name.$age; });
373+
$request = Request::create('/foo/taylor/abc', 'GET');
374+
$this->assertEquals('taylorabc', $router->dispatch($request)->getContent());
359375
}
360376

361377

0 commit comments

Comments
 (0)