在方法调用前后做一些事情如果您想在调用一个方法之前后之后做一些事情,您可以在方法开始或结尾分别调度一个事件:class Foo{ // ... public function send($foo, $bar) { // do something before the method $event = new FilterBeforeSendEvent($foo, $bar); $this->dispatcher->dispatch('foo.pre_send', $event);
允许多个类向另一个类添加方法,您可以在您想要扩展的类中定义神奇的 __call() 方法,像这样:class Foo{ // ... public function __call($method, $arguments) { // create an event named 'foo.method_is_not_found' $event = new HandleUndefinedMethodEvent($this, $method, $arguments); $this->dispatcher->
发送电子邮件对于任何 web 应用程序来说,都是一个经典任务,并且具有特殊的复杂性和潜在的缺陷。不是重新创建轮,发送电子邮件的解决方案之一就是使用 SwiftmailerBundle,利用 Swift Mailer 库的能力。这个 bundle 来自于 Symfony 标准版本。配置使用 Swift Mailer 的话,您需要对您的邮件服务器配置。