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
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 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年前 评论

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