Controller API
The Controller, the main concept of Beego, takes on many responsibilities and can be divided into:
Callbacks or Hooks
When discussing the lifecycle, two concepts should be clarified:
- The lifecycle of the entire Beego application
- The lifecycle of a single request being processed
Here we discuss the second one: the life cycle of a single request being processed.
The Controller customizes several hook functions.
Prepare()
: It will be called before each request is executedFinish()
: It will be called after each request is executed
Interruption
Two methods are provided inside the Controller
.
(c *Controller) Abort(code string)
: This method will return the user response immediately. If the correspondingcode
developer has registered the error handling logic, then the error handling logic will be executed. If not, only an error code will be output. For how to register the handling logic, please refer to Error Handling(c *Controller) CustomAbort(status int, body string)
: This method will output bothstatus
andbody
to the user. Wherestatus
is the HTTP response code. But if the corresponding HTTP response code already has error handling logic registered, then theAbort
method should be used becauseCustomAbort
willpanic
if the response code already has error handling logic registered.
There is also a method StopRun
in the Controller
. This method is more dangerous than the previous two methods because it doesn’t output an error response directly to the user, but happens panic
directly. Then if the user’s RecoverPanic
option is true
, then the logic to recover from recover
will be executed.
RecoverPanic
please refer to the description in web.Config
.