Routing

Controller Routes

Connect HTTP verbs to request-local controller actions.

By Guillermo Alvarez - Published - Updated

Bind a verb

Controller routes use the route scope verb methods:

func Draw(router *lazyroutes.Scope) {
    router.Get("/posts", posts.New, (*posts.PostsController).Index)
    router.Post("/posts", posts.New, (*posts.PostsController).Create)
    router.Get("/posts/{post_id}", posts.New, (*posts.PostsController).Show)
}

Each route receives a constructor and a method expression. GoLazy constructs a fresh controller for each request.

Read path values

Named path values use the standard request API:

func (c *PostsController) Show(
    _ http.ResponseWriter,
    r *http.Request,
) error {
    postID := r.PathValue("post_id")
    c.Set("postID", postID)
    return nil
}

Use this shape for most HTML routes. Use generator arguments only when typed arguments remove useful boilerplate.

Use route helpers

Route names are inferred from method and path. Templates can use path_for:

<a href="{{path_for "posts"}}">Posts</a>
<a href="{{path_for "post" .post.Param}}">{{.post.Title}}</a>

Controllers can use the same named routes:

path, err := c.PathFor("post", post.Param)
if err != nil {
    return err
}
return c.RedirectTo(path, http.StatusSeeOther)

Pass trailing lazycontroller.URLParams when a route needs a query string:

adminPath := c.MustPathFor("admin_post", post.Param, lazycontroller.URLParams{
    "token": post.AdminToken,
})

Use MustPathFor only when the route name and parameters are application invariants. It panics instead of returning a route-helper error.

Controller route metadata is attached to the request before the action runs, so helpers and rendering know the current route, controller, action, and namespace.

Split plain handlers when useful

Use a plain handler route for endpoints that do not need a controller:

router.HandleFunc(http.MethodGet, "/up", func(w http.ResponseWriter, _ *http.Request) error {
    w.WriteHeader(http.StatusNoContent)
    return nil
})

Use HandlerFunc Routes for those endpoints.