Skip to content

Mapping deep nested RESTful controllers

Mat Ryer edited this page Jul 25, 2013 · 1 revision

Often in the RESTful world, you want to map controllers as nested resources of other controllers. Goweb allows you to do this using the RestfulController interface, so you just have to implement the Path() string method on your controller.

For example, if you had books that belonged to people you would want the PeopleController mapped to /people, and the BooksController mapped to /people/1/books.

type BooksController struct{}

func (c *BooksController) Path() string {
  return "people/{person_id}/books"
}

If the BooksController implemented the RestfulReader interface by providing the Read method, it would be mapped in the following way:

Read(id string, ctx context.Context) error -> /people/{person_id}/books/{id}

Then you can get the person_id by a ctx.PathValue("person_id") call.