gin icon indicating copy to clipboard operation
gin copied to clipboard

feat(route): add Engine.PathFor function

Open asbjornu opened this issue 2 years ago • 8 comments

Adds the gin.Engine.PathFor function to retrieve the path of registered handlers. Resolves #3256.

asbjornu avatar Apr 30 '23 21:04 asbjornu

any update ?

syrm avatar Jan 04 '24 15:01 syrm

It's seem not good, it can not called in the handler. Mostly, it will result a package loop.

iflamed avatar Apr 01 '24 12:04 iflamed

@iflamed, can you please post some code to reproduce the problem you're experiencing? It would be good to cover it with a unit test, if applicable.

asbjornu avatar Apr 03 '24 14:04 asbjornu

@iflamed, can you please post some code to reproduce the problem you're experiencing? It would be good to cover it with a unit test, if applicable.

If you place Router := gin.Default() in package 'routePkg', How we use Router in a handler package handlerPkg?

Mostly, we will import handlerPkg in the routePkg, like below:

import handlerPkg

Router.GET("/path/to/go",handlerPkg.SomeHandler)

In handlerPkg, If i want create a path use Router, have to import routePkg, like below:

import routePkg

func SomeHanlder() {
   Router.PathFor(getUser, ":name", "gopher")
}

@asbjornu It's not very convenient. And these code will result a package loop import.

iflamed avatar Apr 03 '24 14:04 iflamed

@iflamed, right, so your issue is that Router.PathFor(getUser, …) would need to have a reference to the getUser handler, which may reside in another package? That may be resolved by providing a Router.PathForName() method, where you pass in the name of the handler as it would be returned by nameOfFunction(handler).

Would that be a good solution for you?

asbjornu avatar Apr 03 '24 21:04 asbjornu

@iflamed, right, so your issue is that Router.PathFor(getUser, …) would need to have a reference to the getUser handler, which may reside in another package? That may be resolved by providing a Router.PathForName() method, where you pass in the name of the handler as it would be returned by nameOfFunction(handler).

Would that be a good solution for you?

No, In fact, I think the most case we use PathForName will be in the handler. So the best way to create a path is use gin.Context, you should add PathForName in the Context. @asbjornu

iflamed avatar Apr 04 '24 01:04 iflamed

@asbjornu Maybe you could learn something from how Laravel framework build path with router.

iflamed avatar Apr 04 '24 01:04 iflamed

move to next v1.11 milestone.

appleboy avatar Apr 04 '24 01:04 appleboy