Laravel 6 将新增为指定队列任务设置中间件的功能
程序员文章站
2022-06-03 13:01:38
taylor otwell 在 laravel 6 中新增了为指定队列任务设置中间件的能力,以便我们在执行某些队列任务之前先执行一些业务逻辑:
this [pull...
taylor otwell 在 laravel 6 中新增了为指定队列任务设置中间件的能力,以便我们在执行某些队列任务之前先执行一些业务逻辑:
this [pull request] adds an easy way to have job specific middleware for queued jobs. global job middleware were actually already possible by calling bus::pipethrough([]) in a service provider during the application boot process…these middleware provide a convenient location to wrap jobs in some logic before they are executed.
我们可以在 job 类中定义 middleware()
方法来设置对应的中间件,该方法返回的是中间件对象实例数组,因此可以定义多个中间件:
public function middleware() { return [new somemiddleware]; }
下面是中间件的示例代码,与之前的中间件定义并无大的区别,只是将 $request 参数替换成了 $command
:
class somemiddleware { public function handle($command, $next) { // do something... return $next($command); } }
此外,还可以在分发任务时动态指定中间件,这些中间件会自动和定义在该任务类的 middleware()
方法返回的中间件合并:
somejob::dispatch()->through([new somemiddleware]);
该特性将会在本月底发布的laravel 6 中提供,你可以在这个 pull request 中查看更多细节。
总结
以上所述是小编给大家介绍的laravel 6 将新增为指定队列任务设置中间件的功能,希望对大家有所帮助