Skip to content

Conversation

@asbjornu
Copy link

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

@syrm
Copy link

syrm commented Jan 4, 2024

any update ?

@appleboy appleboy added the type/bug Found something you weren't expecting? Report it here! label Jan 15, 2024
@appleboy appleboy added this to the v1.10 milestone Jan 15, 2024
@appleboy appleboy changed the title feat: add Engine.PathFor function feat(route): add Engine.PathFor function Mar 22, 2024
@appleboy appleboy added feature and removed type/bug Found something you weren't expecting? Report it here! labels Mar 22, 2024
@iflamed
Copy link

iflamed commented Apr 1, 2024

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

@asbjornu
Copy link
Author

asbjornu commented Apr 3, 2024

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

@iflamed
Copy link

iflamed commented Apr 3, 2024

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

@asbjornu
Copy link
Author

asbjornu commented Apr 3, 2024

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

@iflamed
Copy link

iflamed commented Apr 4, 2024

@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
Copy link

iflamed commented Apr 4, 2024

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

@appleboy appleboy modified the milestones: v1.10, v1.11 Apr 4, 2024
@appleboy
Copy link
Member

appleboy commented Apr 4, 2024

move to next v1.11 milestone.

@appleboy appleboy modified the milestones: v1.11, v1.12 Jun 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create link with router

4 participants