Laravel 5.1 - 从内部调用中获取工匠参数

时间:2017-09-29 21:32:46

标签: php laravel laravel-5 laravel-5.1

我做

当我运行工匠queue:work或工匠queue:listen时,它会运行当前命令及其相应的参数。现在我的问题是,我如何访问这些参数?

正如您在下面的图片中看到的那样,参数存在,但我不知道如何访问它们?

enter image description here

1 个答案:

答案 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