The revel.Controller is the context for a single request and controls
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
}
NOTE:
*revel.Controller
must be 'embedded' as the first type in
a controller struct anonymously.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 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.
ViewArgs map[string]interface{} // Args passed to the template.
Validation *Validation // Data validation helpers
}
// The request
type Request struct {
In ServerRequest
ServerHeader *RevelHeader
ContentType string
Format string // "html", "xml", "json", or "txt"
AcceptLanguages AcceptLanguages
Locale string
WebSocket ServerWebSocket
Method string
RemoteAddr string
Host string
// URL request path from the server (built)
URL *url.URL
// DEPRECATED use GetForm()
Form url.Values
// DEPRECATED use GetMultipartForm()
MultipartForm *MultipartForm
}
// 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.
Extending the Controller
A Controller is any type that embeds revel.Controller either directly or indirectly. This means controllers may extend other classes, here is an example on how to do that.
- Note in the
MyController
theBaseController
reference is NOT a pointer
type (
BaseController struct {
*revel.Controller
}
)
type (
MyController struct {
BaseController
}
)