从 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 自己的包中注册命令的正确方法是什么?
最佳答案
不需要使用服务容器和单例。您可以简单地将命令类放入服务提供商的 register()
部分的 $this->commands([]);
数组中。
像那样:
$this->commands([
Acme\MyCommand::class
]);
关于php - 如何在 Laravel 5 中从包中注册控制台命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29231657/