Skip to content

Commit

Permalink
[11.x] Adds conditional to routes (#53654)
Browse files Browse the repository at this point in the history
* Added conditionals to routes.

* Cs fixes.

* Update Route.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
Boorinio and taylorotwell authored Nov 25, 2024
1 parent fd189ca commit 401798e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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));
}

public function testRoutePrefixParameterParsing()
Expand Down

0 comments on commit 401798e

Please sign in to comment.