Laravel 事件示例
Laravel Event
提供了一些简单的观察器,用来实现用户订阅或者监听 web 应用中触发的各种事件。在 laravel 中,所有的 event 类都保存在 app/Events
文件夹下,所有的监听类被存放在 app/Listeners
文件夹下。
让我们开始吧,看看他们是怎么工作的
//首先在项目目录下执行如下命令
php artisan event:generate
php artisan make:event UserRegisteredEvent
php artisan make:listener SendMailListener --event="UserRegisteredEvent"
上述命令分别生成如下文件:
UserRegisteredEvent.php
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class UserRegisteredEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $title;
public $body;
public $to;
public function __construct($title, $body, $to)
{
$this->title = $title;
$this->body = $body;
$this->to = $to;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
<?php
namespace App\Listeners;
use App\Events\UserRegisteredEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class SendMailListener implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param UserRegisteredEvent $event
* @return void
*/
public function handle(UserRegisteredEvent $event)
{
echo "Start sending email".PHP_EOL;
sleep(2);
echo "Email sended to {$event->to}".PHP_EOL;
}
}
EventServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\UserRegistered' => [
'App\Listeners\SendRegistrationEmail',
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
现在让我们在控制器中添加如下代码来进行测试
for ($i=0; $i < 10; $i++) {
event(new UserRegisteredEvent("hi", "how are you", "alex@gmail.com"));
}
最后一步,执行 queue
php artisan queue:work
Done
[2020-06-17 08:18:57][171] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[2020-06-17 08:18:59][171] Processed: App\Listeners\SendRegistrationEmail
[2020-06-17 08:18:59][172] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[2020-06-17 08:19:01][172] Processed: App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:01][173] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[2020-06-17 08:19:03][173] Processed: App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:03][174] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[2020-06-17 08:19:05][174] Processed: App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:05][175] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[2020-06-17 08:19:07][175] Processed: App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:07][176] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[2020-06-17 08:19:09][176] Processed: App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:09][177] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[2020-06-17 08:19:11][177] Processed: App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:11][178] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[2020-06-17 08:19:13][178] Processed: App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:13][179] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[2020-06-17 08:19:15][179] Processed: App\Listeners\SendRegistrationEmail
[2020-06-17 08:19:15][180] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to alex@gmail.com
[2020-06-17 08:19:17][180] Processed: App\Listeners\SendRegistrationEmail
It works like a charm :)
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
虽然 我英文差 但是我能看的明白代码
这玩意最大的要求就是,要弄懂1对多通知,触发加入队列执行