question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Route current check does not give the expected result when an optional route parameter is on the route

See original GitHub issue

Description

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:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bakerkretzmarcommented, Jan 6, 2021

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!

0reactions
bakerkretzmarcommented, Aug 27, 2021

@sbc640964 not sure what exactly you’re asking for / demonstrating, but feel free to open a detailed issue or PR any time!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found