douteng5673 2017-09-29 21:32
浏览 18

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

I do

when I run artisan queue:work or artisan queue:listen it runs the current commands with their corresponding Arguments. Now my question is, how can I Access those Arguments?

As you can see in the following Picture, the Arguments are there but I have no clue how to Access them?

enter image description here

  • 写回答

1条回答 默认 最新

  • dongya6395 2017-09-29 23:20
    关注

    In a project which follow a "standard project structure"

    You must have a class in app/Console named Kernel which extends Illuminate\Foundation\Console\Kernel, an example of how to implement it is as follows:

    <?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');
        }
    }
    

    so now let's create a new command, call it "print" and it will accept a parameter called text, here is the implementation :

    <?
    
    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'));
        }
    }
    

    as you can see, the new command accept a parameter called text and print it in console.

    So to retrieve the parameter sent to an command call, you have to use the argument method in the follow way:

    $commandInstance->argument('key_of_parameter');
    

    To get more info read the docs

    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存