-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[11.x] Adds conditional to routes #53654
Conversation
@taylorotwell Thank you, much appreciated!! |
Can you please explain how will this feature be useful? Any specific use cases you were thinking of? |
In my case I have a route that's only used in testing, this is perfect for that. |
If you need specific routes for testing, shouldn't you just define them inside the test? framework/tests/Routing/RoutingRouteTest.php Lines 54 to 58 in 5a880cc
instead of hardcoding them inside your project code? |
$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)); |
There was a problem hiding this comment.
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
This pull request introduces the Conditionable trait to the Laravel Route class, allowing developers to add conditional logic when defining routes. Consider the following:
No Breaking Changes
This addition does not alter any existing functionality of the Route class. The Conditionable trait is introduced as an optional feature that will only be applied when explicitly used by developers. Existing routes will continue to work as expected without any modifications.
P.S. tests included.
Thanks for your time