Route current check does not give the expected result when an optional route parameter is on the route
See original GitHub issueDescription
The route().current()
function does not work if there is an optional parameter in the route.
Expected behavior
For example, in my app we use optional parameters so that our users can request different response types like service-agreements.json
, service-agreements.csv
, etc.
Route::prefix('/{participant}/service-agreements{extension?}')->group(function () {
Route::get('/', [ServiceAgreementsController::class, 'index'])->name('participants.service-agreements.index');
});
The route parameter is optional, when no extension is given the response will be HTML/Blade/etc.
With the above example, when visiting /participants/123/service-agreements
, this is the result:
route().current('participants.service-agreements.*')
===false
route().current('participants.service-agreements.index')
===false
The expected result is:
route().current('participants.service-agreements.*')
===true
route().current('participants.service-agreements.index')
===true
We would also expect true
if the URL was /participants/123/service-agreements.html
. (Currently, we get the expected result when .html
is at the end of the URL.)
Environment
- Laravel version: 8.18.1
- Ziggy version: 1.0.4
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Understand Laravel route with optional parameter
Optional parameters work when it is the last URL element so in your case it won't work.
Read more >Routing in ASP.NET Core - Microsoft Learn
The default value is used if no value is present in the URL for the parameter. Route parameters are made optional by appending...
Read more >Routing (Symfony Docs)
In the defaults option of a route you can optionally define parameters not included in the route configuration. This is useful to pass...
Read more >API — Flask Documentation (2.2.x)
The endpoint name for the route defaults to the name of the view function if the endpoint parameter isn't passed. An error will...
Read more >Optional route parameters - Blazor University
When we run this app we see we can navigate either to /counter (no parameter required) or /counter/AnyNumber (parameter value specified). When no...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Okay thanks, that’s really cool actually 😅
We’ll work on adding support for this, I have a feeling it’s not going to be straightforward but hopefully I’m wrong!
@sbc640964 not sure what exactly you’re asking for / demonstrating, but feel free to open a detailed issue or PR any time!