douyan8772 2018-03-28 07:51
浏览 49

如何从命令本身调用Laravel命令句柄?

I have a command like this

class TestCommand extends Command {
 .....
 constructor
 .....

 public function handle()
 {
    ....
    command logic
    ....
 }
}

the TestCommandis registered in the Kernel.php also.

now I need to extend TestCommand,

class ExtendedCommand extends TestCommand {

  public function __construct ()
  {
    parent::__constrct();
    .....
    modify some variables
    .....
  }

  public function sync()
  {
     //trying to call the parent class handle method.

     $this->handle(); // not working & getting PHP Error:  Call to a member function getOption() on null in /home/vagrant/sinbad/vendor/laravel/framework/src/Illuminate/Console/Command.php on line 292
     $this->execute(); // obviously not working & getting TypeError: Too few arguments to function Illuminate\Console\Command::execute(),
  }
}

//instantiating the new command
   new(ExtendedCommand)->sync();

Now I need to call the ExtendedCommand what are the possible ways I can do it? a solution without registering this command in the kernel.php would be better, cause I am not looking for an Artisan::call way.

  • 写回答

1条回答 默认 最新

  • dongpan8439 2018-03-28 07:58
    关注

    More like this

    <?php
    
    class TestCommand extends Command
    {
        public function __construct(Handler $handler)
        {
            parent::__construct();
    
            $this->handler = $handler;
        }
    
        public function handle()
        {
            $this->handler->handle();
        }
    }
    
    // No need to extends from TestCommand
    class ExtendedCommand extends Command
    {
        public function __construct(Handler $handler)
        {
            parent::__construct();
    
            $this->handler = $handler;
    
            // do the rest
        }
    
        public function sync()
        {
            $this->handler->handle();
            $this->execute();
        }
    }
    
    class Handler
    {
        public function handle()
        {
            // Put your logic here
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大