Laravel 5-包中命令的绑定

时间:2019-04-17 15:05:31

标签: laravel composer-php laravel-5.8

我想创建一个包含命令的软件包。给定命令依赖于对依赖库的绑定-这样:

  • 在我指定的composer.json文件中
"extra": {
        "laravel": {
            "providers": [
                "Test\\ServiceProvider"
            ]
        }
    }
  • 创建的服务提供商
<?php

namespace Test;

use Illuminate\Support\ServiceProvider as Provider;
use Test\Commands\TestCommand;
use External\Package\Binding;

class ServiceProvider extends Provider
{
    public function boot()
    {
        if ($this->app->runningInConsole()) {
            $this->commands([
                TestCommand::class,
            ]);   
        }
    }

    public function register()
    {
        $this->app->singleton(Binding::class, function () {
            return new Binding();
        }); 
    }
}
  • 创建的命令
<?php

namespace Test\Commands;

use Illuminate\Console\Command;
use External\Package\Binding;

class TestCommand extends Command
{
    public function __construct(Binding $binding)
    {
        parent::__construct();

        $this->binding = $binding;
    }
}

但是我得到了

  

Illuminate \ Contracts \ Container \ BindingResolutionException:构建[Test \ Commands \ TestCommand]时无法实例化目标[External \ Package \ Binding]

如何解决这个问题?

0 个答案:

没有答案