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

Make part of the path optional, including a string and param #106

Closed
tharakawj opened this issue May 20, 2017 · 7 comments
Closed

Make part of the path optional, including a string and param #106

tharakawj opened this issue May 20, 2017 · 7 comments

Comments

@tharakawj
Copy link

tharakawj commented May 20, 2017

I have set of filters in my path like this.

/goods/size/:s/brand/:b/color/:c

Now I want to make some of these filters optional so that I will be able to match all of the following paths.

/goods
/goods/size/large
/goods/size/large/brand/abc/color/red
/goods/brand/abc
/goods/size/medium/color/green

How can I do something like that? I'm expecting to have something like this.

/goods(/size/:s)?(/brand/:b)?(/color/:c)?

But seems it's not working.

@blakeembrey
Copy link
Member

You can't put params inside of the regexp area. What you can do is write your own regexp. Something like (/size/[^/]+)? would work.

@tharakawj
Copy link
Author

I thought about using regex as well. But then it's hard to identify parameters as regex only output parameters with the index and parameters count may vary.

@blakeembrey
Copy link
Member

You can prefix it with a param name like in the README. It can always be named or unnamed.

@tharakawj
Copy link
Author

I can't find how to name parameters when using regex. Can you give me a small example?

@blakeembrey
Copy link
Member

/:size(size/[^/]+)?

@tharakawj
Copy link
Author

For path /size/medium, it results size/medium for the size parameter and not only 'medium'. But still better than any other solution. Thanks!

@blakeembrey
Copy link
Member

Yes, that's correct. There's no nested matching parameters. I think that's also mentioned in the README (I'm just on mobile now). You'll need to strip the prefix for now, which should be a bit easier with the named params. Nested matching groups is something being considered for the future, but I haven't really figured it out yet (do you end up with two matches? Can you make the outer one non-matching and the inner one matching? How do those params affect indexes or named parameters?). I'll keep this usecase in mind, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants