ZEND-MVC Events

Source: Internet
Author: User
Tags zend

  Zend\mvc\mvcevent is inherited from Zend\eventmanager\eventand Fires when Zend\mvc\application::bootstrap () executes. If your controller implements the Zend\mvc\injectapplicationeventinterface,mvcevent will be injected into these controllers.

Mvcevent will add a fetch and rules for the following objects: Application, Request, Response, Router, routermatch, result (usually the results of the dispatch controller), ViewModel (The view model layout is generally shown). Application, Request, Response, router, and ViewModel are all injected during the bootstrap event. The next route event is injected into the Routematch object to encapsulate the results of the routing. Routematch objects are used throughout MVC, so the route, Request, 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)

The order in which events are triggered:

Name Constant Description
Bootstrap Mvcevent::event_bootstrap To boot application by creating Viewmanager
Route Mvcevent::event_route Performing a route (or routing-related behavior)
Dispatch Mvcevent::event_dispatch Dispatch the matching route to the appropriate controller/behavior
Dispatch.error Mvcevent::event_dispatch_error Triggered when an error occurs during the dispatch process
Render Mvcevent::event_render Prepare the data and delegate the rendering task to the view layer
Render.error Mvcevent::event_render_error Triggers when a render procedure error occurs
Finish Mvcevent::event_finish Once all the things are done, this event triggers the completion of the corresponding task

Detailed Description:

Mvcevent::event_bootstrap ("BOOTSTRAP")

Listener: The Zend\mvc\view\http\viewmanager,onbootstrap method is called.

Role: Prepare the view layer (that is, instantiate Zend\mvc\view\http\viewmanager).

Trigger mode: Zend\mvc\application Bootstrap () method.

Mvcevent::event_route ("ROUTE")

Listener 1:zend\mvc\moduleroutelistener::onroute

Role: Determines whether the module namespace should be added in front of the controller name, mainly to prevent the routing match contains the parameter key matching to the Module_namespace constant

Listener 2:zend\mvc\routelistener::onroute If no route is not matched to Mvcevent::event_dispatch_error will be triggered.

Action: Try to match the request to the router and return a Routematch object.

Trigger mode: Zend\mvc\application::run

Action: If an error occurs during routing, a short loop callback will be used to stop the event from continuing to propagate .

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 suitable for all bad situations. This article does not describe the console environment. The console environment allows you to view official documents.

There are two functions in the class Zend\mvc\view\http\createviewmodellistener as listeners for this event:

1, Createviewmodelfromarray (if the controller behavior returns an associative array, the listener converts the array into a ViewModel object.)

2, Createviewmodelfromnull (when the controller returns a null value, the method converts it to a ViewModel object)

Class Zend\mvc\view\http\routenotfoundstrategy::p Reparenotfoundviewmodel creates and returns a 404ViewModel

The Zend\mvc\view\http\injecttemplatelistener::injecttemplate class injects a template into the view model. The template name inherits the controller name from the routing match (or the action in the Controller)

Class Zend\mvc\view\http\injectviewmodellistener::injectviewmodel inserts a ViewModel and adds it to the Mvcevent object. There are two cases: a) join as a sub-object, including the view model. b) If the result is terminated, replace the default case

The class Zend\mvc\middlewarelistener::ondispatch triggers mvcevent::event_dispatch_error to load and dispatch the matching PSR-7 middleware from the service manager.

Class Zend\mvc\dispatchlistener::ondispatch will trigger the Mvcevent::event_dispatch_error effect as above.

Class Zend\mvc\controller\abstractcontroller::ondispatch This method is an abstract class.

Trigger mode:

Zend\mvc\application::run uses a callback from the short loop to terminate the propagation of the event. (When an error occurs while routing)

Zend\mvc\controller\abstractcontroller::d ispatch If a listener returns a response object, the event propagation is terminated. Every time Abstractcontroller listens to this event, the Ondispatch method is called when it is triggered.

Mvcevent::event_render ("RENDER")

Listener:

Zend\mvc\view\console\defaultrenderingstrategy::render used to render views

Zend\mvc\view\http\defaultrenderingstrategy::render also renders the view, notice the difference with the above environment

Trigger mode:

Zend\mvc\application::competerequest This event is triggered before the Mvcevent::finish trigger.

Mvcevent::event_finish ("FINISH")

Listener:

Zend\mvc\sendresponselistener::sendresponse Trigger Sendresponseevent to prepare response.

Trigger mode:

Zend\mvc\application::run once Mvcevent::route or mvcevent::D ispatch event Returns a correct responseinterface triggers this event

The zend\mvc\application::completerequest is triggered after mvcevent::render (that is, the view has been rendered now).

About the Sendresponse event

Zend\mvc\responsesender\sendresponseevent defines the following methods:

Setresponse ($response)

GetResponse ()

Setcontentsent ()

Contentsent ()

Setheaderssent ()

Headerssent ()

These methods are used to set the answer header and answer content.

Listener:

Zend\mvc\sendresponselistener\phpenvironmentresponsesender::__invoke using the Environment HTTP

Zend\mvc\sendresponselistener\consoleresponsesender::__invoke use the environment as the console.

Zend\mvc\sendresponselistener\simplestreamresponsesender::__invoke

This event is executed after the Mvcevent::finish event is triggered

ZEND-MVC Events

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.