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.

Is there any way to make route().current() also match params?

See original GitHub issue

Expected Behavior

Some way to determine that the route matches, including params. For example, if I’m on any of the following pages, route().current('backend.inventory.show') returns true because the route name is the same for all:

  • /backend/inventory/1/category/2
  • /backend/inventory/2/category/2
  • /backend/inventory/3/category/13
  • /backend/inventory/7/category/1
  • /backend/inventory/1/category/13

Current Behavior

I’m not sure if there is already some method or argument to a method that will allow me to determine this.

For bug reports only please provide

Currently installed Laravel version:

5.8.15

Currently installed Ziggy version

0.7.1

Example route from your Laravel Routes file.

Route::get('/backend/inventory/{tenant}/category/{category}', [Backend\InventoryController::class, 'show'])->name('backend.inventory.show');

Contents of Ziggy.namedRoutes

"backend.inventory.show": {
	"domain": null,
	"methods": [
		"GET",
		"HEAD"
	],
	"uri": "backend/inventory/{tenant}/category/{category}"
}

Ziggy call in context

I have a custom Vue directive that uses the href attribute to determine if the link is to the current page. The link’s href will be a route: <a :href="route('backend.inventory.show'. { tenant: currentTenant, category: category.id })">{{ category.name }}</a>

export default function (el, binding, vNode) {
    let { name } = vNode.data.attrs.href;

    if (name === undefined) {
        return;
    }

    // You can ignore this part, it works as intended for those routes that I DO want to be fuzzy matched.
    if (binding.modifiers.fuzzy) {
        const parts = name.split('.');

        if (parts.length > 1) {
            parts.splice(-1, 1, '*'); // remove last segment of route and replace with fuzzy token
            name = parts.join('.');
        }
    }

    if (route().current(name)) {
        el.classList.add('active');
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
reininkcommented, Jul 1, 2020

I just ran into this myself. It would be awesome if Ziggy handled this out of the box! 👌

4reactions
TheNewSoundcommented, Jan 25, 2020

I would like to see that current() wont return a name but the whole route object, including resolved parameters

Example: Route: /item/{name} Current url: /item/example So I could use current().parameters to get: {name: example}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Match - React Router: Declarative Routing for React.js
A match object contains information about how a <Route path> matched the URL. ... params - (object) Key/value pairs parsed from the URL...
Read more >
React router v6 access route params and pass as props
For this you can simply use a wrapper component to "sip" the route match param and pass it along to your component on...
Read more >
Dynamic Route Matching with Params - Vue Router
Vue Router uses its own path matching syntax, inspired by the one used by express , so it supports many advanced matching patterns...
Read more >
How to Use React Router: useParams | by Megan Lo - Medium
According to the definition in React Router doc, useParams returns: an object of key/value pairs of URL parameters. Use it to access match....
Read more >
Use matchPath to Match Nested Route Paths in Parent Routes ...
There may be a time in your application when you need to compare the previous route id to the current route id to...
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