laravel任务中间件是怎么实现的,事件监听器能否使用中间件? 求大佬解答
看了事件监听器也是实现ShouldQueue接口就可以发送队列,那是不是也可以像任务队列一样可以添加中间件。
任务代码
namespace App\Jobs;
use App\Http\Middleware\RateLimited;
class ConsumeMessage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job
* @return void
*/
public function handle()
{
//handle
}
public function middleware()
{
return [new RateLimited()];
}
}
事件监听器代码
namespace App\Listeners;
use App\Events\BlogView;
use App\Http\Middleware\RateLimited;
use Illuminate\Contracts\Queue\ShouldQueue;
class BlogViewListener implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* @param BlogView $event
* @return void
*/
public function handle(BlogView $event)
{
echo date('Y-m-d H:i:s') . "队列处理 ". $event->event_data['id'] .PHP_EOL;
}
public function middleware()
{
return [new RateLimited()];
}
}
推荐文章: