其他分享
首页 > 其他分享> > laravel 事件&订阅

laravel 事件&订阅

作者:互联网

1、事件&订阅 理解

2、生成事件和订阅

php artisan event:generate
#执行后会生成`app/Events`和`app/Listeners`目录

php artisan make:event IncreaseStockEvent
#生成事件1 -- 增加库存

php artisan make:event ReduceStockEvent
#生成事件2 -- 扣减库存

php artisan make:listener StockSubscribe
#生成订阅者 -- 操作库存

3、注册订阅者

/**
* 需要注册的订阅者类。
* @var array
*/
protected $subscribe = [
	'App\Listeners\StockSubscribe',
];

4、为订阅者绑定事件

public function subscribe($events) {
    $events->listen(
        'App\Events\IncreaseStockEvent',
        'App\Listeners\StockSubscribe@onIncreaseStock'
    );

    $events->listen(
        'App\Events\ReduceStockEvent',
        'App\Listeners\StockSubscribe@onReduceStock'
    );
}

5、事件参数

<?php

namespace App\Events;

//... ...
class IncreaseStockEvent
{
    //... ...
    
    public $userName;
    public $goodsSn;
    public $qty;
    
    public function __construct(string $userName, string $goodsSn, int $qty)
    {
        $this->userName = $userName;
        $this->goodsSn = $goodsSn;
        $this->qty = $qty;
    }
}
<?php

namespace App\Events;

//... ...
class ReduceStockEvent
{
    //... ...
    
    public $userName;
    public $goodsSn;
    public $qty;

    public function __construct(string $userName, string $goodsSn, int $qty)
    {
        $this->userName = $userName;
        $this->goodsSn = $goodsSn;
        $this->qty = $qty;
    }
}

6、订阅者增加业务处理-库存操作

<?php

namespace App\Listeners;


use App\Events\IncreaseStockEvent;
use App\Events\ReduceStockEvent;
use Illuminate\Support\Facades\Log;

class StockSubscribe
{
    /**
     * 为订阅者绑定事件
     *
     * @param  Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events) {
        $events->listen(
            'App\Events\IncreaseStockEvent',
            'App\Listeners\StockSubscribe@onIncreaseStock'
        );

        $events->listen(
            'App\Events\ReduceStockEvent',
            'App\Listeners\StockSubscribe@onReduceStock'
        );
    }

    /**
     * 增加库存
     * @param IncreaseStockEvent $event
     */
    public function onIncreaseStock(IncreaseStockEvent $event)
    {
        //--处理业务逻辑

        //接收事件参数
        $userName = $event->userName ?? '';
        $goodsSn = $event->goodsSn ?? '';
        $qty = $event->qty ?? 0;
        Log::info($userName.' -- '.$goodsSn.' -- '.'增加库存' .' -- '.$qty);
    }

    /**
     * 扣减库存
     * @param ReduceStockEvent $event
     */
    public function onReduceStock(ReduceStockEvent $event)
    {
        //--处理业务逻辑

        //接收事件参数
        $userName = $event->userName ?? '';
        $goodsSn = $event->goodsSn ?? '';
        $qty = $event->qty ?? 0;
        Log::info($userName.' -- '.$goodsSn.' -- '.'扣减库存' .' -- '.$qty);
    }

}

7、调用事件触发订阅行为

event(new \App\Events\IncreaseStockEvent('张三','白衬衫',2));
#增加库存
    
event(new \App\Events\ReduceStockEvent('李四','黑礼服',3));
#扣减库存

8、结果

[2022-05-08 16:22:11] local.INFO: 张三 -- 白衬衫 -- 增加库存 -- 2  

[2022-05-08 16:22:13] local.INFO: 李四 -- 黑礼服 -- 扣减库存 -- 3  

标签:laravel,订阅,userName,--,App,qty,事件,goodsSn,event
来源: https://www.cnblogs.com/pine007/p/16247672.html