Laravel事件的创建运行步骤
Laravel  /  管理员 发布于 4年前   1633
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", "[email protected]"));
}
最后一步是运行队列
php artisan queue:work
效果:
[2020-06-17 08:18:57][171] Processing: App\Listeners\SendRegistrationEmail
Start sending email
Email sended to [email protected]
[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 [email protected]
[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 [email protected]
[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 [email protected]
[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 [email protected]
[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 [email protected]
[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 [email protected]
[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 [email protected]
[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 [email protected]
[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 [email protected]
[2020-06-17 08:19:17][180] Processed: App\Listeners\SendRegistrationEmail
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号