Laravel 5 命令 - 调度命令管道
Laravel 5 Commands - dispatch command pipeline
是否可以在调度命令时创建一种命令管道?
例如:
$this->dispatch(new UploadFileCommand)
会调用 ValidateFileCommand
, WhateverCommand
.
类似于 pipeThrough
但会触发特定命令。
我知道你刚才问过这个问题,但我想我还是会回答。
artisan make:middleware WhateverPipeline
然后您必须添加
use DispatchesCommands;
然后
$this->dispatch(new WhateverCommand());
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Foundation\Bus\DispatchesCommands;
class WhateverPipeline {
use DispatchesCommands;
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->dispatch(new WhateverCommand());
return $next($request);
}
}
WhateverCommand 不必是队列命令,然后在管道中执行。
您还可以使用
从 WhateverCommand 中调度任何命令
use DispatchesCommands;
和一些人交谈后,我意识到我想多了。解决方案比我想象的要简单。要通过命令进行管道传输,只需执行以下操作:
public function whateverMethod(Dispatcher $dispacher) {
$dispached->pipeThrough([]) // arrays with commands
}
Dispacher通过漂亮的laravel 5方法注入而来!
是否可以在调度命令时创建一种命令管道?
例如:
$this->dispatch(new UploadFileCommand)
会调用 ValidateFileCommand
, WhateverCommand
.
类似于 pipeThrough
但会触发特定命令。
我知道你刚才问过这个问题,但我想我还是会回答。
artisan make:middleware WhateverPipeline
然后您必须添加
use DispatchesCommands;
然后
$this->dispatch(new WhateverCommand());
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Foundation\Bus\DispatchesCommands;
class WhateverPipeline {
use DispatchesCommands;
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->dispatch(new WhateverCommand());
return $next($request);
}
}
WhateverCommand 不必是队列命令,然后在管道中执行。
您还可以使用
从 WhateverCommand 中调度任何命令use DispatchesCommands;
和一些人交谈后,我意识到我想多了。解决方案比我想象的要简单。要通过命令进行管道传输,只需执行以下操作:
public function whateverMethod(Dispatcher $dispacher) {
$dispached->pipeThrough([]) // arrays with commands
}
Dispacher通过漂亮的laravel 5方法注入而来!