我做
当我运行工匠queue:work
或工匠queue:listen
时,它会运行当前命令及其相应的参数。现在我的问题是,我如何访问这些参数?
正如您在下面的图片中看到的那样,参数存在,但我不知道如何访问它们?
答案 0 :(得分:1)
在遵循" standard project structure"
的项目中你必须在app / Console中有一个名为Kernel的类,它扩展了Illuminate\Foundation\Console\Kernel,一个如何实现它的例子如下:
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* {@inheritdoc}
*/
protected $commands = [
//here you have to put your commands class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule): void
{
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands(): void
{
require base_path('routes/console.php');
}
}
现在让我们创建一个新命令,将其命名为&#34; print&#34;它将接受一个名为text的参数,这里是实现:
<?
namespace App\Console\Commands;
use Illuminate\Console\Command;
class TestCommand extends Command
{
/**
* {@inheritdoc}
*/
protected $signature = 'test {text}';
/**
* {@inheritdoc}
*/
protected $description = 'Test command.';
/**
* {@inheritdoc}
*/
public function handle()
{
$this->info($this->argument('text'));
}
}
如您所见,新命令接受一个名为text的参数并在控制台中打印。
因此,要检索发送到命令调用的参数,必须按以下方式使用参数方法:
$commandInstance->argument('key_of_parameter');
要获取更多信息,请阅读docs