Symfony2 event Distribution Component

Source: Internet
Author: User

This article introduces the object-oriented design. Each class has a clear division of labor to ensure code flexibility and scalability. Developers can modify the methods by inheriting the parent class. If you want to share the changes with other developers who want to do the same thing, they will not be able to do so through inheritance. For example, if you want to add a plug-in system for your project, the plug-in must be able to add methods, or do other things before or after executing a method. It is difficult to implement it through inheritance. Symfony2 can implement this function by implementing the Mediator mode (mitm. Taking the HttpKernel component as an example, after a response object is created, it is necessary to modify it with other elements of the system (for example, adding a cache Declaration). symfony is implemented by throwing a kernel event: A listener (object) tells the Dispatch Center Object (dispatcher) kernel. at a certain time point, the symfony2 kernel tells the scheduling object to assign the kernel. response event, which uses the Response object as an accessible parameter to pass out the scheduling object to notify all the kernel. response listener, allowing them to modify and install Resonse objects using the official git repository (https://github.com/symfony/EventDispatcher) using composer using method events when an event is assigned out, it needs a unique identifier, it is also the object for all listeners to listen. An event instance is also created and passed to all listeners. Event objects often contain assigned event objects. Naming rules can be any character string that complies with the following rules: lowercase letters, numbers, points, and underscores (_). The name space is followed by a verb and ends with a verb to describe what will happen (for example, a request) for example: kernel. responseform. pre_set_data event name and event object when the scheduling object notifies the listener, it passes the event object as a parameter. The base class of the time object is very simple, and there is only one method to stop the event expansion. Generally, data about a specific event needs to be transmitted along with the event object at the same time, so that the listener can have the required information. When creating an event listener, the event object passed to the listener may be a subclass of the event object, which contains more information that can be extracted and responded to the event. The Dispatcher is a core member of The event distribution system. All in all, only one sender is created to maintain all registered listeners. When an event is assigned, it notifies all listeners who listen to the event. [Php] use Symfony \ Component \ EventDispatcher; $ dispatcher = new EventDispatcher (); to connect a listener to use an existing event, you need to connect the listener, in this way, when a specific event occurs, you can execute the action [php] $ listener = new AcmeListener (); $ dispatcher-> addListener ('foo. action ', array ($ listener, 'onfooaction'); addListener has three parameters: the name of the listener event. When an event is triggered, an optional number is displayed in the execution sequence of an executable php callback function. The higher the event, the more important it is.

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.