Laravel事件的创建运行步骤
Laravel  /  管理员 发布于 1年前   774
Laravel事件的创建运行步骤
看看官网的介绍:
Eloquent 模型触发几个事件,允许你挂接到模型生命周期的如下节点: retrieved、 creating、 created、 updating、 updated、 saving、 saved、 deleting、 deleted、 restoring 和 restored。事件允许你每当特定模型保存或更新数据库时执行代码。每个事件通过其构造器接受模型实例。
retrieved 事件在现有模型从数据库中查找数据时触发。当新模型首次被保存时, creating 和 created 事件被触发。如果数据库中已经存在模型并且调用了 save 方法, updating / updated 事件被触发。这些情况下, saving / saved 事件也被触发。
事件提供了一种简单的观察器实现,该实现允许用户订阅和收听Web应用程序中触发的各种事件。 Laravel中的所有事件类都存储在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"));
}
最后一步是运行队列
php artisan queue:work
效果:
[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
博主 在
hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 @路过的靓仔:cdn静态资源被墙,已修复..GGGGGGGGG 在
layui框架常用输入框介绍中评论 写的很好解决问题..路过的靓仔 在
hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 剩下好多 wait 状态的..激光豆芽 在
为什么你不能安逸?国内996为什么没有国外955香?中评论 国内现在无意义的内卷太多了..激光豆芽 在
阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 厉害了..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号