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

Add support for matching parameters with current() #314

Closed
wants to merge 9 commits into from

Conversation

bakerkretzmar
Copy link
Collaborator

@bakerkretzmar bakerkretzmar commented Aug 7, 2020

This PR adds the ability to pass a second argument to current() with an object containing parameters to check in addition to the route name. Closes #211.

// Route 'events.venues.show': /events/{event}/venues/{venue}
// On https://myapp.com/events/1/venues/2

// Before
route().current() // 'events.venues.show'
route().current('events.venues.show') // true

// After
route().current('events.venues.show', { event: 1, venue: 2 }) // true
route().current('events.venues.show', { event: 2, venue: 6 }) // false

It matches query parameters too:

// On https://myapp.com/events/1/venues/2?user=Amy
route().current('events.venues.show', { event: 1, venue: 2, user: 'Amy' }) // true

current() was also updated to ignore the query string when retrieving the name of the current route:

// On https://myapp.com/events/1/venues/2?user=John

// Before
route().current() // undefined

// After
route().current() // 'events.venues.show'

This implementation has one major caveat: parameters are matched all-or-nothing, including the query string. In the future it would be great if Ziggy supported matching just some parameters, but for now it will check everything you pass in:

// On https://myapp.com/events/1/venues/2
route().current('events.venues.show', { event: 1 }) // false

// On https://myapp.com/events/1/venues/2?user=Amy
route().current('events.venues.show', { event: 1, venue: 2 }) // false

Thanks to @sbine for helping with a bunch of the regular expression stuff in here!

@bakerkretzmar bakerkretzmar added enhancement in progress We're working on it labels Aug 7, 2020
@bakerkretzmar bakerkretzmar added this to the v1.0 milestone Aug 7, 2020
@bakerkretzmar bakerkretzmar self-assigned this Aug 7, 2020
@bakerkretzmar bakerkretzmar deleted the jbk/current-route-with-params branch October 9, 2020 13:56
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
Development

Successfully merging this pull request may close these issues.

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