Title: Controllers Desc: Controller is responsible for handling incoming request, validate user inputs, call appropriate application logic behind and returns the response for that request. Keywords: controller, request, response, reply, view arg, cookies, extending controller
Controller
is responsible for handling incoming request, validate user inputs, call appropriate application logic behind and returns the response for that request. The response would be different content types based on request such as HTML
, JSON
, XML
, Text
, Binary
, File
, etc.
A Controller
is any struct
type that embeds the aah.Context
(Note: this is not a standard package context.Context
). The controller gets access to -
// Examples
// App controller
type App struct {
*aah.Context
}
// User Controller
type User struct {
*aah.Context
}
Request contains processed incoming request details such as Params
, Content Type
, Locale
, Client IP
(aka Remote IP/Address) Accept Content Type
, Accept Encoding
, Raw
request object, etc.
- To learn more about
Request
attributes and methods click here. - To learn more about
Params
attributes and methods click here
Response implements interface of http.CloseNotifier
, http.Flusher
, http.Hijacker
, http.Pusher
and handy methods.
Note:
- It is recommend to use
Reply
builder to compose your response. - If you're using
cxt.Res
directly to send reply/response, don't forget to callReply().Done()
so that framework will not intervene with your response.
aah provides intuitive way to compose response using Reply()
builder in chained method calls. To learn more click here
In aah framework default session mode is stateless
, of-course via configuration you can enable stateful
. Framework does HMAC
sign and AES
encryption to secure session data.
It is perfect for Web and API application-
Web
requires to persist state info between the HTTP requestAPI
doesn't requires state info between the HTTP request.
To learn more about Session
object click here.
i18n messages is for internationalization and localization. To learn more click here.
aah framework provides a convenient way to create reverse route URL via two methods.
ReverseURL
- method generates the URL for given route name (defined in theroutes.conf
) and arguments.ReverseURLm
- method generates the URL for given route name (defined in theroutes.conf
) andmap
of named arguments. Remaining provided values added as URL Query parameters.
// let's say route named `login` has path `/login.html`
ReverseURL("login")
// Output
"//<host-name:port-no-if-present>/login.html"
// let's say route named `user_info` has path `/v1/users/:userId`
ReverseURL("user_info", 100001)
// Output
"//<host-name:port-no-if-present>/v1/users/100001"
// let's say route named 'product_info' has path `/product/:productId`
ReverseURLm("product_info", map[string]interface{}{
"id": "5564HFGFG56207",
"lang": "es",
})
// Output
"//<host-name:port-no-if-present>/product/5564HFGFG56207?lang=es"
AddViewArg
method adds the given key
and value
into View Arguments. These values are accessible on templates. Chained call is possible. Also you can supply bunch of values via Reply().Ok().HTML(aah.Data{ ... })
.
u.AddViewArg("Username", "Jeevanandam M.").
AddViewArg("UserId", "e5f0396dfc504a949d9f9e2c511a779c")
aah.Context enables you to identify the current incoming request is static or application route using IsStaticRoute()
method. For e.g: if you have register custom NotFound
handler in the route config, this method is very handy.
// true - static route
// false - application route
a.IsStaticRoute()
A Controller
is any struct
type that embeds the aah.Context
directly or indirectly. You can extend the controller for your need.
Note: when extending a controller via indirectly, embedding is not a pointer.
Example
// App controller
type App struct {
*aah.Context
}
// User Controller
type User struct {
App
}