-
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
make uri_for receive a name for a route #33
Comments
For fun I'm tackling this issue right now. I was thinking about the possibilities to implement it. I was thinking about modifying some D2::C::DSL methods, namedly GET, POST, PUT, PATCH, HEAD, OPTIONs etc to at the end return the $regex path. After that we could create a keyword that wraps those HTTP verbs, like: named 'my_route' => get '/path' => sub {} This way the keyword named would receive the params 'my_route' and '/path' (because the get keyword returns the path at the end) and we could get that into a hash, no problem. The problem is with the uri_for method. It would make sense NOT to create a separate named_uri_for, but use a common uri_for for both named and unnamed matches. The problem is, that the D2::C::Request package, where the uri_for lies has no awareness about it's surrounding, it does not have a reference to app nor dsl, so I have trouble getting the hash of named matches there. Any thoughs on this? BTW I know, that the HTTP verb keywords (get, post etc) take an $options hash as an optional parameter, where technically could the route name go, but that hash is reserved for HTTP header options, so I'd rather not mix it in there. Maybe a prettier syntax could be achieved, but that would require some internal changed in Dancer2. |
Funny that I'm looking at it too right now and didn't remember there was an issue for it already. I was thinking of calling it a route alias. I like the An easier way to handle it before the I'd be really happy to get more suggestions here. |
Now that I think about it, if this would not fit into the Core, it could be easily a plugin as well.. |
I think this might not easily be done in a plugin because you would have to have access to aliases in the dispatching mechanism. It'd be easier to put it in core by adding another attribute to the route and then fetching that from the dispatcher. |
I've been playing with this tonight, and given #1088 merging, the following gist provides a simple implementation of it. I would be happy to put this in a plugin, or possibly in core, but we would need to resolve two things:
There are two additional cases I'm not sure how and if to handle:
|
First and foremost, I like the idea. Makes things a little more tidy Thoughts and suggestions:
you can do around like 29 of the gist:
where |
btw, the second bullet above also takes care of |
If we were considering this for core, adding it as an attribute in the Route object would be easier. I was trying to make it decoupled from the Route object. Giving an identifier for Routes I think makes sense, and I've suggested that in #1088. Two additional quick comments:
|
On 2016-01-02 12:14 PM, Sawyer X wrote:
Then use
:-( That gives me the sads, as it prevents things like
|
On Sat, Jan 2, 2016 at 6:49 PM, Yanick Champoux
That's a good idea! That can solve that part.
It wasn't a final decision, just what I figured at the time. :) Happy to be find myself wrong.
Hmm... is this a practical example? Aren't you just using |
On 2016-01-02 03:13 PM, Sawyer X wrote:
It's not from a real app, but it's something I do from time to time.
And there is the case where I can pass many values, which number and
for
Nope. Just like the other named routes, I could decide to rebase It's not a as-common case as the named parameters, but actively |
Alright. Convinced. :) We still need to pick the best naming for everything. Coding is easy, naming is hard. :/ |
I like |
|
Sounds good. Maybe |
Hi Sawyer X, I know, you are busy being the new perl Pumpkin. But are there any knews on this issue? |
We're pushing hard to finish the plugins work for this release (hopefully going out on Sunday, tomorrow). I can try and prepare a sample to test out today and tomorrow. I can't promise. Otherwise, it will have to wait until the next release. Once this next release it out, it will be easier to continue making more releases. This last one took us a long long time to get exactly right, which is why no production releases were done for a while. |
@mickeyn suggested adding a set of keywords to be named, such as
(If |
I was just asking. Really, don't rush it. Do this releases plugins work and if this feature makes it into the next release, it will be just fine. I like named_get, named_post, named_out, named_del too. As long as the intent is clear enough, I can live with it. And I think named_... is clear enough. I don't know about uri_for though. Doesn't already understand more than one parameter? I thought, the second parameter already does what I initially described? (turns a hash into parameters) |
Let's bump this back up! :) |
I've given this a bit more thought. I think we don't need new DSL keywords. There are only two ways to call
We can also add the following:
We can easily find out how many were sent. The last elements should be |
Simple an elegant, I like it. Should also work with |
I wrote the sample code. I'll put up a PR for discussion. |
PR pushed: #1270. :) |
I updated the PR with a new keyword: |
This is now officially resolved as implemented and released in version 1.1.0, available on CPAN. :) |
Thanks @xsawyerx for implementing and releasing. |
This is something for the wishlist, not an issue itself.
Right now uri_for converts a relative path to an absolute path and attaches parameters if necessary.
In case you have to change a path in your controller, you have to search all your templates, to find where this path was used and change your templates accordingly.
My idea would be to provide a name for a route and use that name to identify the route to generate a uri. Something along the lines:
The Benefit would be, that we can change the route in our controller without having to search our templates for where this route was used. Like so:
The idea is, to decouple the uris in templates from routing in the controller, so that you are more flexible to change your routes. And to make uri generation more comfortable.
The text was updated successfully, but these errors were encountered: