The revel.Controller is the context for a single request and controls - the incoming Request stuff - and the Response back, in Hmtl, Json, Xml, File or your own custom.

A Controller is any type that embeds a *revel.Controller as the first field/type.

type MyAppController struct {
    *revel.Controller
}
type MyOtherController struct {
    *revel.Controller
    OtherStuff string
    MyNo int64
}
type FailController struct {
    XStuff string
    *revel.Controller // Fail as it should be first    
}
Very Important: *revel.Controller must be 'embedded' as the first type in the a controller struct anonymously, the Go way for 'kinda inheritance'

The revel.Controller is the context for a request and contains the Request and Response data.

Below are the most used components and type/struct definitions to give a taste of *revel.Controller, Request, Params and Response.

type Controller struct {
    Name          string          // The controller name, e.g. "Application"
    Type          *ControllerType // A description of the controller type.
    MethodType    *MethodType     // A description of the invoked action type.
    AppController interface{}     // The controller that was instantiated.

    Request  *Request
    Response *Response
    Result   Result

    Flash      Flash                  // User cookie, cleared after 1 request.
    Session    Session                // Session, stored in cookie, signed.
    Params     *Params                // Parameters from URL and form (including multipart).
    Args       map[string]interface{} // Per-request scratch space.
    RenderArgs map[string]interface{} // Args passed to the template.
    Validation *Validation            // Data validation helpers
}
type Request struct {
    *http.Request
    ContentType string
    Format          string // "html", "xml", "json", or "txt"
    AcceptLanguages AcceptLanguages
    Locale          string
    Websocket       *websocket.Conn
}
// These provide a unified view of the request params.
// Includes:
// - URL query string
// - Form values
// - File uploads
type Params struct {
    url.Values
    Files map[string][]*multipart.FileHeader
}
type Response struct {
    Status      int
    ContentType string
    Headers     http.Header
    Cookies     []*http.Cookie
    Out http.ResponseWriter
}
  • As part of handling a HTTP request, Revel instantiates an instance of a revel.Controller.
  • It then sets all of the properties on the embedded revel.Controller.
  • Revel does not share a Controller instance between requests.

GitHub Labels