如何使用 Laravel 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13370865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Laravel events?
提问by luckytaxi
I want to be notified when a new user signs up. In my controller I fire an event as follows.
我想在新用户注册时收到通知。在我的控制器中,我按如下方式触发事件。
Event::fire('myapp.new_user', array($this->data['user']->email));
Where do I define the listener
?
我在哪里定义listener
?
Event::listen('myapp.new_user', function ($uid) {
Message::to("[email protected]")
->from("[email protected]", "My App")
->subject('New User')
->body("new user")
->html(true)
->send();
});
How does the listener
know an event fired?
怎么listener
知道一个事件被触发了?
回答by William Cahill-Manley
You need to make sure your listeners are defined prior to your application logic executing, when events are thrown they can only be caught by already registered listeners, it does not look for new ones.
你需要确保你的监听器在你的应用程序逻辑执行之前定义,当事件被抛出时,它们只能被已经注册的监听器捕获,它不会寻找新的监听器。
On small projects I just place my listeners in application/start.php
at the bottom of the file. This file happens before your routes are ran and it serves as sort of a application config file with some logic to it. You need to place these events towards the bottom of the file, at least after the Autoloader mappings have been registered.
在小项目中,我只是将我的听众放在application/start.php
文件的底部。这个文件在你的路由运行之前发生,它作为一种应用程序配置文件,其中包含一些逻辑。您需要将这些事件放在文件底部,至少在注册自动加载器映射之后。
On Larger projects I will create application/listeners.php
and require this file inside application/start.php
for better readability.
在较大的项目中,我将创建application/listeners.php
并要求在内部application/start.php
使用此文件以提高可读性。
Hope this helps!
希望这可以帮助!
回答by Ihab Shoully
try :
尝试 :
Event::listen('myapp.new_user', function ($uid) {
Message::to("[email protected]")
->from("[email protected]", "My App")
->subject('New User')
->body("new user")
->html(true)
->send();
return 'test my event';
});
回答by Darren Taylor
You can also define classes to handle specific events and then use a Service Provider to register them.
您还可以定义类来处理特定事件,然后使用服务提供者来注册它们。
Below is a basic example:
下面是一个基本的例子:
app/NewUserListener.php
应用程序/NewUserListener.php
The listener which is called when the event is fired:
触发事件时调用的侦听器:
class NewUserListener
{
public function handle($uid)
{
// Send an email here
}
}
app/ListenerServiceProvider.php
应用程序/ListenerServiceProvider.php
The ServiceProvider - remember and add this to the list of Service Providers in L4 Config.
ServiceProvider - 记住并将其添加到 L4 Config 中的服务提供者列表中。
use Illuminate\Support\ServiceProvider;
class ListenerServiceProvider extends ServiceProvider
{
public function register()
{
Event::listen('myapp.new_user', 'NewUserListener');
// Register more events here...
}
}
If you organize listeners etc. into appropriately named folders, it ends up being a lot easier to maintain should you have a bunch of listeners later. You can also instantiate and test the listeners if you write them in this manner.
如果您将侦听器等组织到适当命名的文件夹中,如果您以后有一堆侦听器,最终维护起来会容易得多。如果您以这种方式编写它们,您还可以实例化和测试侦听器。