Evg*_*niy 11 php packages laravel-5
从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'
];
}
}
Run Code Online (Sandbox Code Playgroud)
我的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');
}
}
Run Code Online (Sandbox Code Playgroud)
我在app.php中注册服务提供程序并执行dump-autoload.但是当我尝试执行时
php artisan eternaltree:install
Run Code Online (Sandbox Code Playgroud)
它告诉我
[InvalidArgumentException] There are no commands defined in the "eternaltree" namespace.
Run Code Online (Sandbox Code Playgroud)
我认为我的命令没有被服务提供商注册,因为php artisan list没有显示我的命令.任何人都可以向我解释在Laravel 5中在自己的包中注册命令的正确方法是什么?
Ifn*_*not 27
不需要使用服务容器和单例.您只需将命令类放入服务提供程序$this->commands([]);的register()部分即可.
像那样 :
$this->commands([
Acme\MyCommand::class
]);
Run Code Online (Sandbox Code Playgroud)
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
FooCommand::class,
BarCommand::class,
]);
}
}
Run Code Online (Sandbox Code Playgroud)
If向您的服务提供商类添加类似的语句。
我找到了一个决定,很简单:
在registerInstallCommand()
添加
$this->commands('command.eternaltree.install');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7209 次 |
| 最近记录: |