Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add route regex registration methods #34997

Merged
merged 3 commits into from
Oct 28, 2020
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
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/PendingResourceRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class PendingResourceRegistration
{
use Macroable;
use Macroable, RouteRegexConstraintTrait;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// This works
Route::apiResource('foo', 'FooController')->whereString('foo');


/**
* The resource registrar.
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

class Route
{
use Macroable, RouteDependencyResolverTrait;
use Macroable, RouteDependencyResolverTrait, RouteRegexConstraintTrait;

/**
* The URI pattern the route responds to.
Expand Down
48 changes: 48 additions & 0 deletions src/Illuminate/Routing/RouteRegexConstraintTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Illuminate\Routing;

trait RouteRegexConstraintTrait
{
/**
* Apply a route regex requirement to the route.
*
* @param array $parameters
* @param string $regex
* @return void
*/
protected function applyRouteRegex(array $parameters, string $regex)
{
foreach ($parameters as $parameter) {
is_string($parameter)
? $this->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;
}
}
26 changes: 26 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down