Search code examples
gobuffalo

How to add new route to autogenerate resources?


please help me, I want add new route/func to generate resource (etc. users).

app.Resource("/users", UsersResource{})

I found func addRoute, but doesn't work.

func (a *App) addRoute(method string, url string, h Handler) *RouteInfo { ...

My idea is something like this.

"/users/handleSomething/idOfUser"

Right now i have only this func:

List(Context)
Show(Context)
New(Context)
Create(Context)
Edit(Context)
Update(Context)
Destroy(Context)

Thx for your time and help :)

PS: Sorry for my English :(


Solution

  • If you want additional routes you can use the methods GET,POST,PUT, ... that are defined on the *App type. (https://gobuffalo.io/en/docs/routing#supported-http-methods)

    To handle dynamic path segments in the route you can use named parameters. (https://gobuffalo.io/en/docs/routing#named-parameters)

    Example:

    app.PATCH("/users/handleSomething/{id}", handleSomethingHandler)
    

    To retrieve the value of the named parameter you can use c.Param("id") where c is the buffalo context passed in to your handler.