Skip to content
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

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

Closed
mikemand opened this issue Apr 30, 2019 · 7 comments · Fixed by #330
Closed

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

mikemand opened this issue Apr 30, 2019 · 7 comments · Fixed by #330
Assignees
Labels
enhancement in progress We're working on it
Milestone

Comments

@mikemand
Copy link

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');
    }
}
@DanielCoulbourne
Copy link
Contributor

This is a good idea. I mayyyyy have time for it tomorrow, but I can't be sure and if not it will be a couple weeks.

@m1guelpf
Copy link
Contributor

m1guelpf commented Sep 5, 2019

@DanielCoulbourne Sorry to bother you, but is there any progress on this? :D

@TheNewSound
Copy link

TheNewSound commented 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}

@jakebathman jakebathman added enhancement in progress We're working on it labels Apr 24, 2020
@jakebathman jakebathman added this to the v1.0 milestone Apr 24, 2020
@reinink
Copy link
Contributor

reinink commented Jul 1, 2020

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

@bakerkretzmar bakerkretzmar added help wanted and removed in progress We're working on it labels Jul 20, 2020
@bakerkretzmar bakerkretzmar modified the milestones: v1.0, v1.1 Jul 20, 2020
@bakerkretzmar bakerkretzmar added the in progress We're working on it label Aug 7, 2020
@bakerkretzmar
Copy link
Collaborator

bakerkretzmar commented Aug 7, 2020

@TheNewSound interesting idea, can you make a new issue for that feature specifically?

Just remembered this is already possible, it's route().params. See the tests for an example.

@bakerkretzmar
Copy link
Collaborator

bakerkretzmar commented Oct 9, 2020

This was added in #330 and will be available shortly in Ziggy 1.0 💪🏻

@mikemand
Copy link
Author

mikemand commented Oct 9, 2020

That's so awesome! Thank you @bakerkretzmar. Also, hurray for 1.0 release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement in progress We're working on it
Projects
None yet
7 participants