The revel.Controller is the context for a single request and controls

  • the incoming Request stuff
  • and the Response back, in Html, Json, Xml, File or your own custom.
    A Controller is any type that embeds a *revel.Controller as the first field/type.
  1. typeMyAppControllerstruct{*revel.Controller}typeMyOtherControllerstruct{*revel.ControllerOtherStuffstringMyNoint64}typeFailControllerstruct{XStuffstring*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.

  1. typeControllerstruct{Namestring// The controller name, e.g. "Application"Type*ControllerType// A description of the controller type.MethodType*MethodType// A description of the invoked action type.AppControllerinterface{}// The controller that was instantiated.Request*RequestResponse*ResponseResultResultFlashFlash// User cookie, cleared after 1 request.SessionSession// Session, stored in cookie, signed.Params*Params// Parameters from URL and form (including multipart).Argsmap[string]interface{}// Per-request scratch space.ViewArgsmap[string]interface{}// Args passed to the template.Validation*Validation// Data validation helpers}
  1. // The request typeRequeststruct{InServerRequestServerHeader*RevelHeaderContentTypestringFormatstring// "html", "xml", "json", or "txt"AcceptLanguagesAcceptLanguagesLocalestringWebSocketServerWebSocketMethodstringRemoteAddrstringHoststring// URL request path from the server (built)URL*url.URL// DEPRECATED use GetForm()Formurl.Values// DEPRECATED use GetMultipartForm()MultipartForm*MultipartForm}
  1. // These provide a unified view of the request params.// Includes:// - URL query string// - Form values// - File uploadstypeParamsstruct{url.ValuesFilesmap[string][]*multipart.FileHeader}
  1. typeResponsestruct{StatusintContentTypestringHeadershttp.HeaderCookies[]*http.CookieOuthttp.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 the BaseController reference is NOT a pointer
  1. type(BaseControllerstruct{*revel.Controller})type(MyControllerstruct{BaseController})
GoDoc Reference
GitHub Labels

原文: https://revel.github.io/manual/controllers.html