Coder Social home page Coder Social logo

Reverse url about echo HOT 12 CLOSED

labstack avatar labstack commented on May 3, 2024
Reverse url

from echo.

Comments (12)

zbindenren avatar zbindenren commented on May 3, 2024

+1

from echo.

patrickdappollonio avatar patrickdappollonio commented on May 3, 2024

+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.

fundon avatar fundon commented on May 3, 2024

+1 url path helper

from echo.

emilgpa avatar emilgpa commented on May 3, 2024

+1

from echo.

vishr avatar vishr commented on May 3, 2024

I would like to know some practical example of this case. Please point me to some frameworks API supporting it.

from echo.

saml avatar saml commented on May 3, 2024

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:

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.

patrickdappollonio avatar patrickdappollonio commented on May 3, 2024

Here's another framework who has this functionality: http://laravel.com/docs/5.0/routing#named-routes

from echo.

vanng822 avatar vanng822 commented on May 3, 2024

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.

vishr avatar vishr commented on May 3, 2024

@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.

newoga avatar newoga commented on May 3, 2024

@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.

newoga avatar newoga commented on May 3, 2024

Also, a couple of other suggestions...

  1. 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.

  1. 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.

newoga avatar newoga commented on May 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.