Comments (12)
+1
from echo.
+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...
from echo.
+1 url path helper
from echo.
+1
from echo.
I would like to know some practical example of this case. Please point me to some frameworks API supporting it.
from echo.
One practical use case is when I want to write a RESTful API with hyperlinks.
For example, using HAL , I would want resource representation to include various links to other states client can go from here.
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 UrlFor
method as well.
Some routing frameworks that support reverse url:
- http://www.gorillatoolkit.org/pkg/mux#Route.URL
- http://flask.pocoo.org/docs/0.10/api/#flask.url_for
- https://playframework.com/documentation/2.3.x/ScalaRouting#Reverse-routing
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).
from echo.
Here's another framework who has this functionality: http://laravel.com/docs/5.0/routing#named-routes
from echo.
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
Maybe something?
from echo.
@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
from echo.
@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?
from echo.
Also, a couple of other suggestions...
- Maybe rather than the
URI()
method accepting a...string
, how about it accepts...interface{}
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 Stringer
interface. That way if you pass a UUID
object to to the reverse routing method, it could still work.
- Also, It might be nice to support sending a map to the reverse route method, such as...
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
})
from echo.
Okay, I closed my original pull request and created a new one #37, this one actually fixes the issue when you call the URI()
method on the right instance of *Echo
.
That being said, I think it would be useful to maintain some kind of link between instances of *Echo
and child *Echo
when created by calling Group()
. Not quite sure what the right API would be for this, but it would be nice if I could call the root *Echo.URI()
and it would know how to create the URI for all handler on itself and its child *Echo
.
To test this, we would have to add back the two checks in the TestEchoURL
method:
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.
from echo.
Related Issues (20)
- Add an AllowContentType middleware (based on chi)
- Bind() panics when the destination is a map with non-interface types HOT 1
- Add SSE function in Context.Response HOT 7
- Any software design issues HOT 1
- Request support for more routing path matching HOT 1
- Unable to use proxy middleware along with bodyDump middleware HOT 1
- `Host` header always blank HOT 6
- Does echo support i18n? [feature request] HOT 2
- Weirdness with wrapped handler and embed FS (static files) HOT 1
- Remove default charset from 'application/json' Content-Type header
- echo v4 websocket client closed, program exited HOT 3
- router not setting ContextKeyHeaderAllow for group, causing cors preflight to not work HOT 6
- Static file has wrong body returned when using a custom HTTP Error Handler and complex paths HOT 2
- QueryParams seem to override formValues HOT 3
- DefaultHTTPErrorHandler does not log HOT 3
- Support Casbin Authorization Middleware HOT 1
- Echo response object calls flush on unflushable objects HOT 7
- Feature request: support Yaml responses transparently. HOT 1
- static middleware: path ist unescaped twice for file names, leading to not downloadable content
- CSRF middleware form lookup consumes all the request body HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from echo.