-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Reverse url #12
Comments
+1 |
+1! This, while it looks like nothing, could make a huge difference maintaining big websites, and you can be confident that any route change you won't need to go through your entire code base to see what to change... |
+1 url path helper |
+1 |
I would like to know some practical example of this case. Please point me to some frameworks API supporting it. |
One practical use case is when I want to write a RESTful API with hyperlinks. Of course I can write methods for calculating urls for various resources. But it involves duplicate work. If I change route, I need to change my relevant Some routing frameworks that support reverse url:
Play one is interesting cause routes are defined in DSL and generates reverse route functions during compilation. If routes are specified using URI Template, it's known to be reversible: https://github.com/jtacoma/uritemplates (see Expand). |
Here's another framework who has this functionality: http://laravel.com/docs/5.0/routing#named-routes |
It is convenient to bind this kind of method to request/router but there is a drawback when you have small number applications/services which need those urls. We had some extra implement/workaround to make thing work. I tried to fix it just providing a route manager https://github.com/vanng822/r2router/blob/master/example/routemanager.go |
@saml, @patrickdappollonio, @fundon: Let me know how it looks. Further, as I have a map of handler and url, do you think an API to return it will be helpful? https://github.com/labstack/echo/blob/master/echo_test.go#L237 |
@vishr, Just submitted a pull request with a couple more tests. It seems like this has some issues when the handlers are paired with a group. Was the API meant to work this way? |
Also, a couple of other suggestions...
This way we can pass any type of value and determine how it should become a string. For example, we should be able to determine if the interface that was passes implements the
e.Get("/users/:id/:files:/fid", getFile)
e.URL(getUser, map[string]interface{}{
"id": "1", // string works
"fid": 2, // int works
"other_id": guid, // as long as it implements `String()` it would work
}) |
Okay, I closed my original pull request and created a new one #37, this one actually fixes the issue when you call the That being said, I think it would be useful to maintain some kind of link between instances of To test this, we would have to add back the two checks in the e := New()
getGroups := func(*Context) {}
getGroup := func(*Context) {}
eg := e.Group("/groups")
eg.Get("", getGroups)
eg.Get("/:id", getGroup)
// These two tests would currently fail
if e.URI(getGroups) != "/groups" {
t.Error("uri should be /groups")
}
if e.URI(getGroup, "1") != "/groups/1" {
t.Error("uri should be /groups/1")
}
// These two tests currently pass
if eg.URI(getGroups) != "/groups" {
t.Error("uri should be /groups")
}
if eg.URI(getGroup, "1") != "/groups/1" {
t.Error("uri should be /groups/1")
} In my opinion, ideally we can have it setup so all 4 pass. |
Needs a function that behaves as follows:
The text was updated successfully, but these errors were encountered: