laravel event事件 讲解

event
注册简单event
首先在 EventServiceProvider中的属性$listen添加事件和监听
  protected $listen = [
        'App\Events\openEvent' => [//事件
            'App\Listeners\openListener',//监听器
        ],
    ];
    执行php artisan event:generate
    会生成 events/App\Events\openEvent.php 和Listeners/App\Listeners\openListener
    在events/App\Events\openEvent.php
    中添加
    public function __construct($show)
    {
        $this->show = $show;
    }
    则此时的 events属性show 就是 事件标识

    在Listeners/App\Listeners\openListener 下的
    public function handle(openEvent $event)
    {
       
    }
    此event 就是事件对象,$event->show 就是我们的事件标识

    在控制器方法中使用
    use App\Events\showEvent;
   use Event;


    Event::fire(new showEvent('事件标识'));或者全局函数
    event(new showEvent('事件标识'));

    此时监听器就会自动执行并根据事件的标识执行不同的操作

    2. 手动定义事件
    在 providers/EventServiceProvider.php
    public function boot()
    {
        parent::boot($events);
        Event::listen('event.show', function($listen){// 如果定义的是单个事件,闭包接受一个参数,
如果十多个event.*必报两个参数
            dd($listen);
    });
ps event.show 是我们定义的事件标识 闭包参数使我们传递的事件数据

党控制器调用Event::fire('event.show',['show1']);时  则会执行
providers/EventServiceProvider.php 相对应的事件 并把第二个参数传到 闭包函数

    在控制器中执行 Event::fire('event.show',['show1']);

    3. 为防止事件堵塞程序 ,使用队列列化的事件 只需要使监听器实现ShouldQueue 接口
    namespace App\Listeners;

   use App\Events\showEvent;
   use Illuminate\Queue\InteractsWithQueue;
   use Illuminate\Contracts\Queue\ShouldQueue;

   class showListener implements ShouldQueue

    4.事件的合集 事件订阅器

    在EventServiceProvider中添加

    protected $subscribe = [
        'App\Listeners\subscribetListener',//注册事件订阅器
    ];


    事件订阅器实现
namespace App\Listeners;


class subscrListener
{
    /**
     * 处理用户登录事件。
     */
    public function onUserLogin($event) {

    }

    /**
     * 处理用户注销事件。
     */
    public function onUserLogout($event) {

    }

    /**
     * 注册侦听器的订阅者。
     *
     * @param  Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            'App\Events\UserLoggedIn',
            'App\Listeners\subscrListener@onUserLogin'//UserLoggedIn 事件的监听者是 onUserLogin 方法
        );

        $events->listen(
            'App\Events\UserLoggedOut',
            'App\Listeners\subscrListener@onUserLogout'//UserLoggedOut 事件的监听者是 onUserLogout 方法
        );
    }

}


控制器调用
        Event::fire(new UserLoggedIn('UserLoggedIn'));
       
        Event::fire(new UserLoggedOut('UserLoggedOut'));





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Laravel事件案例,以便更好地了解Laravel事件的使用: 1. 定义事件Laravel中,您可以使用`make:event` Artisan命令来创建一个事件类。例如,我们将创建一个名为`OrderShipped`的事件类: ``` php artisan make:event OrderShipped ``` 这将在`app/Events`目录中创建一个名为`OrderShipped`的事件类。 2. 定义事件发布者 事件发布者是触发事件的类。在我们的案例中,让我们假设`Order`类在订单发货时触发`OrderShipped`事件。我们可以在`Order`类中使用Laravel的`dispatch`方法来发布事件: ```php use App\Events\OrderShipped; class Order { public function ship() { // 订单发货逻辑 // 发布事件 event(new OrderShipped($this)); } } ``` 3. 定义事件订阅者 事件订阅者是响应事件的类。在我们的案例中,让我们定义一个类来记录日志并发送电子邮件: ```php namespace App\Listeners; use App\Events\OrderShipped; class SendShipmentNotification { public function handle(OrderShipped $event) { // 记录日志 Log::info('Order shipped with ID: ' . $event->order->id); // 发送电子邮件 Mail::to($event->order->customer->email)->send(new OrderShippedEmail($event->order)); } } ``` 在上面的代码中,我们定义了一个`SendShipmentNotification`类来订阅`OrderShipped`事件。当事件被触发时,`handle`方法将被调用,记录日志并发送电子邮件。 4. 注册事件订阅者 最后,我们需要在Laravel应用程序中注册事件订阅者。我们可以在`EventServiceProvider`类中使用`$listen`属性来注册订阅者: ```php namespace App\Providers; use Illuminate\Support\Facades\Event; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider { protected $listen = [ OrderShipped::class => [ SendShipmentNotification::class, ], ]; public function boot() { parent::boot(); } } ``` 在上面的代码中,我们将`OrderShipped`事件订阅到`SendShipmentNotification`类。这将导致`SendShipmentNotification`类在`OrderShipped`事件被触发时执行其`handle`方法。 现在,当我们在`Order`类中调用`ship`方法时,它将触发`OrderShipped`事件并导致`SendShipmentNotification`类执行其`handle`方法,记录日志并发送电子邮件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值