可以从Laravel中的Controller执行异步任务吗?我曾尝试过使用Queues,但我不能。
路线:
Route::post('/loadimages', LoadImagesController@load')->name('loadimages');
控制器:
public function load(Request $request)
{
$urls_array = preg_split("/[\s]+/", $request->urls);
**// I want this task in the background...**
DownloadImagesJob::dispatch($urls_array);
**// ... and return immediately**
return view('home');
}
ShouldQueue:
class DownloadImagesJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $urls;
public function __construct(array $urls)
{
$this->urls = $urls;
}
public function handle()
{
// El código que quiero que se ejecute asíncronamente.
}
}
我正在使用"数据库"驱动程序所以在config / queues.php中:
'default' => env('QUEUE_DRIVER', 'database'),
和
php artisan queue:table
php artisan migrate
但是任务没有异步运行。