php – 如何在Laravel 5中从包中注册console命令?

从Laravel 5开始,我感兴趣的是 – 如何在Laravel 5中注册和使用来自包的控制台命令.

正如laracast讨论https://laracasts.com/discuss/channels/tips/developing-your-packages-in-laravel-5一样,我创建了一个目录结构,将我的包添加到自动加载并创建一个服务提供者

<?php namespace Goodvin\EternalTree;

use Console\InstallCommand;
use Illuminate\Support\ServiceProvider;

class EternalTreeServiceProvider extends ServiceProvider
{
    public function boot()
    {

    }

    public function register()
    {
        $this->registerCommands();
    }

    protected function registerCommands()
    {
        $this->registerInstallCommand();
    }

    protected function registerInstallCommand()
    {
        $this->app->singleton('command.eternaltree.install',function($app) {

            return new InstallCommand();
        });
    }

    public function provides()
    {
        return [

            'command.eternaltree.install'
        ];
    }
}

我的InstallCommand.php脚本存储在/ src / Console中

<?php namespace Goodvin\EternalTree\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class InstallCommand extends Command {

    protected $name = 'install:eternaltree';
    protected $description = 'Command for EternalTree migration & model install.';

    public function __construct()
    {
        parent::__construct();
    }

    public function fire()
    {
        $this->info('Command eternaltree:install fire');
    }

}

我在app.php&注册服务提供商执行dump-autoload.但是当我尝试执行时

php artisan eternaltree:install

它告诉我

[InvalidArgumentException] There are no commands defined in the "eternaltree" namespace.

我认为我的命令没有被服务提供商注册,因为php artisan list没有显示我的命令.任何人都可以向我解释在Laravel 5中在自己的包中注册命令的正确方法是什么?

不需要使用服务容器和单例.您只需将命令类放入$this->命令([]);数组到服务提供者的register()部分.

像那样 :

$this->commands([
    Acme\MyCommand::class
]);

相关文章

laravel的dd函数不生效怎么办
看不懂laravel文档咋办
安装laravel框架出现command怎么办
Laravel开发API怎么使用事务
laravel怎么构建复杂查询条件
laravel如何实现防止被下载