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

[11.x] Adds conditional to routes #53654

Merged
merged 3 commits into from
Nov 25, 2024
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
3 changes: 2 additions & 1 deletion src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Routing\Matching\UriValidator;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use Laravel\SerializableClosure\SerializableClosure;
Expand All @@ -27,7 +28,7 @@

class Route
{
use CreatesRegularExpressionRouteConstraints, FiltersControllerMiddleware, Macroable, ResolvesRouteDependencies;
use Conditionable, CreatesRegularExpressionRouteConstraints, FiltersControllerMiddleware, Macroable, ResolvesRouteDependencies;

/**
* The URI pattern the route responds to.
Expand Down
20 changes: 20 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,26 @@ public function testWherePatternsProperlyFilter()
});
$route->where('bar', '[0-9]+');
$this->assertFalse($route->matches($request));

/*
* Conditional
*/
$route = new Route('GET', '{subdomain}.awesome.test', function () {
//
});

$route->when(true, function ($route) {
$route->whereIn('subdomain', [
'one',
'two',
]);
});

$request = Request::create('test.awesome.test', 'GET');
$this->assertFalse($route->matches($request));

$request = Request::create('one.awesome.test', 'GET');
$this->assertTrue($route->matches($request));
Comment on lines +954 to +965

Choose a reason for hiding this comment

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

This test doesn't actually test if when() works but if matches() works—this is counterintuitive to have in this PR

}

public function testRoutePrefixParameterParsing()
Expand Down