Replies: 2 comments
|
$(cat <<'ENDOFBODY'
e.URI(getUser, "42") // looks up the route registered with getUser handler
e.GET("/users/:id", getUser).Name = "get-user"
e.Reverse("get-user", "42") // → "/users/42"Use // register with a name
usersRoute := e.GET("/users/:id", getUser)
usersRoute.Name = "get-user"
// generate URLs anywhere
url := e.Reverse("get-user", userId)Short version: |
|
The two APIs share the same lookup underneath. When a route is registered, its default route := e.GET("/users/:id", myHandler)
fmt.Println(e.URI(myHandler, "42")) // /users/42
route.Name = "user-detail"
fmt.Println(e.URI(myHandler, "42")) // empty string
fmt.Println(e.Reverse("user-detail", "42")) // /users/42So your observation is correct; naming a route changes what The relevant implementation is just three lines: |
Uh oh!
There was an error while loading. Please reload this page.
Hello, I'm not sure if this is expected behavior or I'm using it wrong, but when I set the name for a route (
e.GET("", myHandler).Name = "MyName") I'm not able to usee.URI(myHandler)anymore and have to usee.Reverse("MyName")instead. I was expecting URI to continue working, can't see why one should affect the other.Thanks in advance.
All reactions