thanks all
I have been learning symfony2 recently, and now I can’t understand the EventDispatcher component, and the official documentation is not very clear. . Solve
Familiar with jQuery? The event mechanism is always the same:
(1) First define the name of the event
This is like the onclick of js, it is just a recognition. You can define an event name such as: vendor.my_event. For ease of use, you can use an "enumeration" class to record these events:
final class Events
{
const MY_EVENT = 'vendor.my_event';
// 其他event……
}
You can use Events::MY_EVENT in the code instead of writing the string directly. It is not necessary to build this "enumeration" class, but it is recommended:
If you make a mistake, an error will be reported directly to facilitate debugging
Centralize it for easy maintenance (otherwise you will forget what events there are)
(2) Define an event class (context definition)
Using jQuery, we generally don’t define event classes ourselves, but in fact this event class also exists (the default one is used). In jQuery’s event callback, the first parameter accepted is the object of the event class, and this object will carry Some contextual stuff like e.target.
Using sf's EventDispatcher, you define your own event class to perform type checking and provide context for callbacks (in layman's terms, what data can be obtained from the event object).
// 这个是sf为你提供的一个基础类
use Symfony\Component\EventDispatcher\Event;
// 你的事件类
class SomeEvent extends Event
{
public function __cosntruct()
{
// 按需定义你的事件类
}
}
(3) Trigger event
You have an event name and an event class, and sf has already equipped you with an event dispatcher. You can use it directly to trigger events:
Familiar with jQuery? The event mechanism is always the same:
(1) First define the name of the event
This is like the onclick of js, it is just a recognition. You can define an event name such as: vendor.my_event. For ease of use, you can use an "enumeration" class to record these events:
You can use Events::MY_EVENT in the code instead of writing the string directly. It is not necessary to build this "enumeration" class, but it is recommended:
(2) Define an event class (context definition)
Using jQuery, we generally don’t define event classes ourselves, but in fact this event class also exists (the default one is used). In jQuery’s event callback, the first parameter accepted is the object of the event class, and this object will carry Some contextual stuff like e.target.
Using sf's EventDispatcher, you define your own event class to perform type checking and provide context for callbacks (in layman's terms, what data can be obtained from the event object).
(3) Trigger event
You have an event name and an event class, and sf has already equipped you with an event dispatcher. You can use it directly to trigger events:
(4) Define a listener (callback)
(5) Make the callback listen to the corresponding event
You can add callbacks using code:
You can also use configuration, that is, dependency injection (DIC):