Zend-MVC event, zendframeworkmvc
Zend \ Mvc \ MvcEvent inherits from Zend \ EventManager \ Event and is triggered when Zend \ Mvc \ Application: bootstrap () is executed. If your controller implements Zend \ Mvc \ InjectApplicationEventInterface, MvcEvent will be injected into these controllers.
MvcEvent adds collectors and rules for the following objects: Application, Request, Response, Router, RouterMatch, and Result (usually the Result of the scheduling controller) and ViewModel (usually display the layout of the view model ). Application, Request, Response, Router, and ViewModel are all injected during bootstrap events. The following route event is injected with the RouteMatch object to encapsulate the result of routing. The RouteMatch object is used in the entire MVC, so the Route, Request, and Response objects are usually obtained through RouteMatch.
MvcEvent also defines the following methods:
setApplication($application)getApplication()setRequest($request)getRequest()setResponse($reponse)getResponse()setRouter($router)getRouter()setRouteMatch($routeMatch)getRouteMatch()setResult()getResult()setViewModel($viewModel)getViewModel()isError()setError()getError()getController()setController($name)getControllerClass()setControllerClass($class)
Event trigger sequence:
Name |
Constant |
Description |
Bootstrap |
MvcEvent: EVENT_BOOTSTRAP |
Guide application by creating ViewManager |
Route |
MvcEvent: EVENT_ROUTE |
Route execution (or routing-related behavior) |
Dispatch |
MvcEvent: EVENT_DISPATCH |
Schedule the matched route to the corresponding Controller/Behavior |
Dispatch. error |
MvcEvent: EVENT_DISPATCH_ERROR |
It is triggered when an error occurs during scheduling. |
Render |
MvcEvent: EVENT_RENDER |
Prepare data and delegate the rendering task to the view layer |
Render. error |
MvcEvent: EVENT_RENDER_ERROR |
Triggered when a render process error occurs |
Finish |
MvcEvent: EVENT_FINISH |
Once all the tasks are completed, this event is triggered to complete the corresponding tasks. |
Details:
MvcEvent: EVENT_BOOTSTRAP ("bootstrap ")
Listener: Zend \ Mvc \ View \ Http \ ViewManager. The onBootstrap method is called.
Purpose: Prepare the View layer (that is, instantiate Zend \ Mvc \ View \ Http \ ViewManager ).
Trigger method: Zend \ Mvc \ Application bootstrap () method.
MvcEvent: EVENT_ROUTE ("route ")
Listener 1: Zend \ Mvc \ ModuleRouteListener: onRoute
Role: determines whether the module namespace should be added before the Controller name, mainly to prevent the parameter keys in route matching from matching the MODULE_NAMESPACE constant.
Listener 2: Zend \ Mvc \ RouteListener: onRoute will be triggered if no route matches MvcEvent: EVENT_DISPATCH_ERROR.
Purpose: try to match the request to the vro and return a RouteMatch object.
Trigger method: Zend \ Mvc \ Application: run
Purpose: if an error occurs during the routing process, a short loop callback is used to stop the continuous event propagation.
MvcEvent: EVENT_DISPATCH ("dispatch ")
There are two types of listeners: one is limited to the Console environment, the other is limited to the HTTP environment, and the listener is applicable to all bad environments. This article does not introduce the CONSOLE environment. You can view official documents in the console environment.
The class Zend \ Mvc \ View \ Http \ CreateViewModelListener contains two functions as the event listener:
1. createViewModelFromArray (if the Controller returns an associated array, the listener converts the array to a ViewModel object.
2. createViewModelFromNull (if the Controller returns a null value, this method converts it to a ViewModel object)
Class Zend \ Mvc \ View \ Http \ RouteNotFoundStrategy: prepareNotFoundViewModel is created and a 404 ViewModel is returned.
Class Zend \ Mvc \ View \ Http \ InjectTemplateListener: injectTemplate injects a template into the View model. The Template Name is inherited from the Controller name (or the action in the Controller) that matches the route)
Class Zend \ Mvc \ View \ Http \ InjectViewModelListener: injectViewModel insert a ViewModel and add it to the MvcEvent object. There are two cases: a) Add as a sub-object, including view model. B) Replace the default value when the result is terminated.
Class Zend \ Mvc \ MiddlewareListener: onDispatch will trigger MvcEvent: EVENT_DISPATCH_ERROR, load and schedule matching PSR-7 middleware from the service manager.
Class Zend \ Mvc \ DispatchListener: onDispatch will trigger MvcEvent: EVENT_DISPATCH_ERROR.
Class Zend \ Mvc \ Controller \ AbstractController: onDispatch this method is an abstract class.
Trigger method:
Zend \ Mvc \ Application: run uses short loop callback to terminate event propagation. (When an error occurs during a route)
Zend \ Mvc \ Controller \ AbstractController: dispatch if a listener returns a Response object, the event will be aborted. The onDispatch method is called whenever the AbstractController listens to this event and is triggered.
MvcEvent: EVENT_RENDER ("render ")
Listener:
Zend \ Mvc \ View \ Console \ DefaultRenderingStrategy: render used to render the View
Zend \ Mvc \ View \ Http \ DefaultRenderingStrategy: render renders the View as well. Note the differences with the preceding environment.
Trigger method:
Zend \ Mvc \ Application: competeRequest this event is triggered before MvcEvent: FINISH is triggered.
MvcEvent: EVENT_FINISH ("finish ")
Listener:
Zend \ Mvc \ SendResponseListener: sendResponse triggers SendResponseEvent to prepare response.
Trigger method:
Zend \ Mvc \ Application: run Once the MvcEvent: ROUTE or MvcEvent: DISPATCH event returns a correct ResponseInterface, this event will be triggered
Zend \ Mvc \ Application: completeRequest is triggered after MvcEvent: RENDER (that is, the view has been rendered at this time ).
About SendResponse events
Zend \ Mvc \ ResponseSender \ SendResponseEvent defines the following methods:
SetResponse ($ response)
GetResponse ()
SetContentSent ()
ContentSent ()
SetHeadersSent ()
HeadersSent ()
These methods are used to set the response header and content.
Listener:
Zend \ Mvc \ SendResponseListener \ PhpEnvironmentResponseSender ::__ invoke environment HTTP
Zend \ Mvc \ SendResponseListener \ ConsoleResponseSender ::__ invoke uses the console environment.
Zend \ Mvc \ SendResponseListener \ SimpleStreamResponseSender ::__ invoke
MvcEvent: this event is executed after the FINISH event is triggered.