Laravel 中使用 PHP artisan xx:xx 命令时,如何调用 Commands 类中的 handle 方法的?


1: 定义自己 Commands类
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Http\Controllers\xxx\xx;

class xx extends Command
{
    protected $signature = 'xx:xx';

    public function handle()
    {
        $obj = new xx();
        $obj->method();
    }
}

2:控制台输入 

 php artisan xx:xx

问题:如何程序自动调用了 handle 方法 ???

追加问题:handle 和 fire 区别???
Do one thing at a time, and do well.
Chris1010
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 6

L5.5中 \Illuminate\Console\Command::execute()源码:

 /**
     * Execute the console command.
     *
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
     * @return mixed
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        return $this->laravel->call([$this, 'handle']);
    }
6年前 评论

L5.5 中已经删除了fire(),根据上面的源码。所以只能使用handle()

6年前 评论
Chris1010

@lx1036 谢谢你的回答, 对于execute方法 我是不是可以修改了 fire 就可以兼容

6年前 评论
Chris1010

@lx1036 对于在控制台 程序是如何触发这个 execute ??? 大佬

6年前 评论

@谭重涛

Illuminate\Foundation\Console\Kernel::handle() -> 
Symfony\Component\Console\Application::run() -> Symfony\Component\Console\Application::doRun()
-> Symfony\Component\Console\Application::doRunCommand() -> Symfony\Component\Console\Command\Command::run() ->   Symfony\Component\Console\Command\Command::execute()
6年前 评论
Chris1010

谢谢大佬! 已经了解了。

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!