-
Notifications
You must be signed in to change notification settings - Fork 277
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
Named routes #1270
Named routes #1270
Conversation
fffcf0d
to
6ce9112
Compare
(rebased from master) |
Any comments from the @PerlDancer team? |
So does this mean if the user does:
and expects something like |
Wouldn't you have to provide |
We can also try to either revise the syntax, like:
Or add a new keyword:
|
We can also detect that if the second parameter is a hashref, the first parameter is a name, not a path. |
Second param can already be a hashref:
|
I think I'd prefer a new |
Yeah a new keyword would be less confusing and prevents nasty surprises. |
Alright, so update: In introduced a new keyword for the However: (and following is a summary of 047d858)
|
This looks very handy! Any news? Has anyone been running on this branch and noticed any gotchas? |
You can now give names to the routes: get NAME, PATH, sub {...}; Or the lesser-known one: get NAME, PATH, OPTIONS_HASHREF, sub {...}; The PATH, as before, can be a string (matching our spec) or a full regular expression. If you do not give a name, a globally incrementing number will be used. I'm not sure that's the best idea but it's a unique identified that, if leaks, does not say which App this is part of. This should lend in the future to allow `uri_for` (or a different DSL keyword) to use the name of a route instead of the path: End result should look something like this: # MyApp.pm get 'view_product', '/view/product/:id' => sub {...}; # in template: <% uri_for( 'view_product', { id => 4 } ); %> We're getting there...
Alright, rethinking this a bit, I find the most confusing part the conflation between URI parameters and Route parameters:
Notice query parameters were not supported here in
That feels like quite a lot. I'm not really happy with it, but I'm not sure how to shorten here and keep it clean. Then there's handling
I'm proposing this as the syntax. Them mixing them:
At the very least, the query parameters are unlikely to be used in conjunction with named parameters because named parameters usually replace query parameters. I still feel like they need to be supported instead of ignored. Last item to address is the method. I chose GET as the only supported one, but since we're replacing route parameters, there's no reason to not support other methods:
However, now the query parameters are arguably wrong to be used:
However, I don't want to start being restrictive and check what method it is to determine if we throw or get mad about the query parameters being provided. What we can do is support the third argument being missing:
Lastly, we need to make sure there can only be one route with the same name, as pointed out by @cxw42 in a comment.
I think this covers all my thoughts. |
Addendum: Routes defined with regexp directly cannot have their arguments expanded. This would require
And in case this seems like
I would've made it throw when trying to define a regexp route with a name, but I can understand the value of giving them all names, even for consistency and readability, not to mention introspection. |
047d858
to
cdb7d13
Compare
Okay, rebased and rewrote everything. Here is what we have now:
Lots of tests, updated documentation. I'm also testing for various failures:
Last things to check before fully signing off:
|
cdb7d13
to
959b72f
Compare
Fixed all the tests. |
959b72f
to
2ff2d53
Compare
Using it in templates is now supported as well:
(This is an example with all possible arguments.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks very much!
Some points re. b4707f0's metadata:
- the commit summary ends with a trailing colon --- is something missing from the summary line?
- The author date is in 2016!
- the commit message still says "This should lend in the future to allow
uri_for
(or a different DSL keyword)". Now that 2ff2d53 implementsuri_for_route
, I think this piece of the message should be removed or modified. - the commit messages says "globally incrementing number". However, it looks like 2ff2d53 changes that to leave routes anonymous unless a name is given. Therefore, I suggest removing the
$count
logic from b4707f0 and removing the relevant text from the commit message.
This new DSL provides a uri_for()-style resolution, but uses named routes for this. get 'view_product' => '/view/:product/:id' => sub {...}; get 'scary' => '/*/:foo/**' => sub {...}; # somewhere else in your App my $uri = uri_for_route( 'view_product' => { 'product' => 'phone', 'id' => 'K2V3', }); # $uri = /view/phone/K2V3 $uri = uri_for_route( 'view_product', { 'foo' => 'bar', 'splat' => [ 'baz', ['quux'] ], }, { 'id' => 4 }, ); # /baz/bar/quux?id=4 * This works on any non-HEAD method (GET, POST, PATCH, PUT, DELETE, and if you create your own). * Splat and Megasplat are supported. Mixing it with named params is also supported. * Query parameters are supported. * HTML escaping is supported. * `request.uri_for_route()` in templates is also supported. * Lots of testing. * Documentation updated.
2ff2d53
to
1211dbf
Compare
I appreciate your thorough review!
This is a common practice of mine. When a commit summary line has a colon, it means I have commit text. When I don't add a commit content, I don't end it with a colon. So, on purpose.
Yup. I amended, which kept this data.
This is, to me, a piece of history. I intended to put it in
Same. |
316b12d
to
81c28ef
Compare
81c28ef
to
2a86388
Compare
2a86388
to
1f702da
Compare
Make sure to document usage from a template:
|
Done. Also updated the article. I think it's ready to merge. |
👏 This looks great. Thanks for walking me through this. An emphatic 👍 from me. |
@racke What do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really good 👍
Excellent! I'll work on getting a release put together over the next couple of days. Thanks everyone! |
Ready for release. Thanks everyone! |
[This related to GH #33.]
This PR allows a user to name their routes and then use a new keyword
uri_for_route()
to generate a path to that route with a LOT of control over how it's constructed.Giving names:
Using it in code:
This also works in templates: