Home >php教程 >php手册 >Zend-MVC events

Zend-MVC events

WBOY
WBOYOriginal
2016-08-04 08:53:171228browse

 ZendMvcMvcEvent inherits from ZendEventManagerEvent and is triggered when ZendMvcApplication::bootstrap() is executed. If your controllers implement ZendMvcInjectApplicationEventInterface, MvcEvent will be injected into these controllers.

  MvcEvent will add getters and rules for the following objects: Application, Request, Response, Router, RouterMatch, Result (usually the result of dispatching the controller), ViewModel (generally showing the view model layout). Application, Request, Response, Router and ViewModel are all injected during the bootstrap event process. The next route event will be injected into the RouteMatch object to encapsulate the routing results. RouteMatch objects are used throughout MVC, so Route, Request, and Response objects are usually obtained through RouteMatch.

MvcEvent also defines the following methods:

setApplication(<span style="color: #800080;">$application</span><span style="color: #000000;">)
getApplication()
setRequest(</span><span style="color: #800080;">$request</span><span style="color: #000000;">)
getRequest()
setResponse(</span><span style="color: #800080;">$reponse</span><span style="color: #000000;">)
getResponse()
setRouter(</span><span style="color: #800080;">$router</span><span style="color: #000000;">)
getRouter()
setRouteMatch(</span><span style="color: #800080;">$routeMatch</span><span style="color: #000000;">)
getRouteMatch()
setResult()
getResult()
setViewModel(</span><span style="color: #800080;">$viewModel</span><span style="color: #000000;">)
getViewModel()
isError()
setError()
getError()
getController()
setController(</span><span style="color: #800080;">$name</span><span style="color: #000000;">)
getControllerClass()
setControllerClass(</span><span style="color: #800080;">$class</span>)

The order in which events are triggered:

Name Constant Description
bootstrap MvcEvent::EVENT_BOOTSTRAP Boot the application by creating a ViewManager
route MvcEvent::EVENT_ROUTE Execute routing (or routing-related actions)
dispatch  MvcEvent::EVENT_DISPATCH Scheduling the matched route to the corresponding controller/behavior
dispatch.error MvcEvent::EVENT_DISPATCH_ERROR It will be triggered when an error occurs during the scheduling process
render MvcEvent::EVENT_RENDER Prepare data and delegate rendering tasks to the view layer
render.error MvcEvent::EVENT_RENDER_ERROR Triggered when an error occurs in the render process
finish MvcEvent::EVENT_FINISH Once everything is completed, this event triggers the corresponding task

Detailed introduction:

MvcEvent::EVENT_BOOTSTRAP("bootstrap")

Listener: ZendMvcViewHttpViewManager, the onBootstrap method will be called.

Function: Prepare the view layer (that is, instantiate ZendMvcViewHttpViewManager).

Trigger method: ZendMvcApplication bootstrap() method.

MvcEvent::EVENT_ROUTE("route")

Listener 1: ZendMvcModuleRouteListener::onRoute

Function: Determines whether the module namespace should be added in front of the controller name, mainly to prevent the parameter key contained in the route matching from matching the MODULE_NAMESPACE constant

Listener 2: ZendMvcRouteListener::onRoute  If no route is matched, MvcEvent::EVENT_DISPATCH_ERROR will be triggered.

Function: Try to match the request to the router and return a RouteMatch object.

Trigger method: ZendMvcApplication::run

Function: If an error occurs during the routing process, a short loop callback will be used to stop the continuous propagation of events.

MvcEvent::EVENT_DISPATCH("dispatch")

Listeners are divided into two categories: one is limited to the console environment, the other is limited to the HTTP environment, and there are listeners applicable to all environments. This article does not introduce the CONSOLE environment. For console environment, you can check the official documentation.

There are two functions in class ZendMvcViewHttpCreateViewModelListener as listeners for this event:

1. createViewModelFromArray (If the controller action returns an associative array, this listener converts the array into a ViewModel object.

2. createViewModelFromNull (if the controller returns a null value, this method will convert it into a ViewModel object)

Class ZendMvcViewHttpRouteNotFoundStrategy::prepareNotFoundViewModel creates and returns a 404ViewModel

Class ZendMvcViewHttpInjectTemplateListener::injectTemplate injects a template into the view model. The template name is inherited from the controller name matched by the route (or the action in the controller)

Class ZendMvcViewHttpInjectViewModelListener::injectViewModel Inserts a ViewModel and adds it to the MvcEvent object. There are two situations: a) Added as a child object, including view model. b) Replace the default situation if the result can be terminated

Class ZendMvcMiddlewareListener::onDispatch will trigger MvcEvent::EVENT_DISPATCH_ERROR, load and dispatch the matching PSR-7 middleware from the service manager.

Class ZendMvcDispatchListener::onDispatch will trigger MvcEvent::EVENT_DISPATCH_ERROR, which has the same effect as above.

Class ZendMvcControllerAbstractController::onDispatch This method is an abstract class.

Trigger method:

ZendMvcApplication::run uses short-circuit callbacks to terminate event propagation. (When an error occurs during routing)

ZendMvcControllerAbstractController::dispatch If a listener returns a Response object, event propagation will be terminated. Whenever AbstractController listens to this event, the onDispatch method will be called when triggered.

MvcEvent::EVENT_RENDER("render")

Listener:

ZendMvcViewConsoleDefaultRenderingStrategy::render is used to render views

ZendMvcViewHttpDefaultRenderingStrategy::render also renders the view, please note the difference from the above environment

Trigger method:

ZendMvcApplication::competeRequest This event is triggered before MvcEvent::FINISH is triggered.

MvcEvent::EVENT_FINISH("finish")

Listener:

ZendMvcSendResponseListener::sendResponse triggers SendResponseEvent to prepare response.

Trigger method:

ZendMvcApplication::run This event will be triggered once the MvcEvent::ROUTE or MvcEvent::DISPATCH event returns a correct ResponseInterface

ZendMvcApplication::completeRequest is triggered after MvcEvent::RENDER (that is, the view has been rendered at this time).

About SendResponse event

ZendMvcResponseSenderSendResponseEvent defines the following methods:

setResponse($response)

getResponse()

setContentSent()

contentSent()

setHeadersSent()

headersSent()

These methods are used to set response headers and response content.

Listener:

ZendMvcSendResponseListenerPhpEnvironmentResponseSender::__invoke uses environment HTTP

ZendMvcSendResponseListenerConsoleResponseSender::__invoke uses the environment console.

ZendMvcSendResponseListenerSimpleStreamResponseSender::__invoke

MvcEvent::This event is executed after the FINISH event is triggered

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn