diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index 5ab78b3f08da..1a7f2f8fd8c6 100644 --- a/src/Illuminate/Routing/PendingResourceRegistration.php +++ b/src/Illuminate/Routing/PendingResourceRegistration.php @@ -7,7 +7,7 @@ class PendingResourceRegistration { - use Macroable; + use Macroable, RouteRegexConstraintTrait; /** * The resource registrar. diff --git a/src/Illuminate/Routing/Route.php b/src/Illuminate/Routing/Route.php index f377e9ff8e4b..283c2d9f57e5 100755 --- a/src/Illuminate/Routing/Route.php +++ b/src/Illuminate/Routing/Route.php @@ -21,7 +21,7 @@ class Route { - use Macroable, RouteDependencyResolverTrait; + use Macroable, RouteDependencyResolverTrait, RouteRegexConstraintTrait; /** * The URI pattern the route responds to. diff --git a/src/Illuminate/Routing/RouteRegexConstraintTrait.php b/src/Illuminate/Routing/RouteRegexConstraintTrait.php new file mode 100644 index 000000000000..ae0f93515b5d --- /dev/null +++ b/src/Illuminate/Routing/RouteRegexConstraintTrait.php @@ -0,0 +1,48 @@ +where([$parameter => $regex]) + : $this->applyRouteRegex($parameter, $regex); + } + } + + /** + * Set a number as regular expression requirement on the route. + * + * @param string $parameters + * @return $this + */ + public function whereNumber(...$parameters) + { + $this->applyRouteRegex($parameters, '[0-9]+'); + + return $this; + } + + /** + * Set any character between a-z or A-Z as regular expression requirement on the route. + * + * @param string|array $parameter + * @return $this + */ + public function whereAlpha(...$parameters) + { + $this->applyRouteRegex($parameters, '[a-zA-Z]+'); + + return $this; + } +} diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 2f97be3e03e5..6596ff0355dd 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -594,6 +594,32 @@ public function testResourceWheres() } } + public function testWhereNumberRegistration() + { + $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+']; + + $this->router->get('/{foo}/{bar}')->whereNumber(['foo', 'bar']); + $this->router->get('/api/{bar}/{foo}')->whereNumber('bar', 'foo'); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + + public function testWhereAlphaRegistration() + { + $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; + + $this->router->get('/{foo}/{bar}')->whereAlpha(['foo', 'bar']); + $this->router->get('/api/{bar}/{foo}')->whereAlpha('bar', 'foo'); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + public function testCanSetRouteName() { $this->router->as('users.index')->get('users', function () {