当我在Laravel 5.3中构建我的第一个依赖注入类时,我得到了一个BindingResolutionException

时间:2016-12-28 08:01:59

标签: php laravel dependency-injection laravel-5.3

这是我的代码:

class WechatServiceProvider extends ServiceProvider
{
    // Bootstrap the application services.
    // @return void
    public function boot()
    {
        //
    }

    // Register the application services.
    // @return void
    public function register()
    {
        $this->app->bind('App\Http\Wechat',function()
        {
            $arr = array('token'=>'foo') ;
            return new Wechat($arr) ;
        });
    }
}

尝试在此处自动注入依赖项

class WechatController extends Controller
{
    protected $wechatObj;

    public function __construct(Wechat $wechatObj)
    {
        $this->wechatObj = $wechatObj;
    }
    ...
}

在App \ Http \ Wechat.php

public function __construct($options)
{
    $this->token = isset($options['token'])?$options['token']:'';
    $this->encodingAesKey = isset($options['encodingaeskey'])?$options['encodingaeskey']:'';
    $this->appid = isset($options['appid'])?$options['appid']:'';
    $this->appsecret = isset($options['appsecret'])?$options['appsecret']:'';
    $this->agentid = isset($options['agentid'])?$options['agentid']:'';
    $this->debug = isset($options['debug'])?$options['debug']:false;
    $this->logcallback = isset($options['logcallback'])?$options['logcallback']:false;
}

错误堆栈

    Container.php第850行中的
  • BindingResolutionException : 类App \ Http \ Wechat中无法解析的依赖项解析[参数#0 [$ options]]
  • 在Container.php第850行
  • 在Container-> resolveNonClass(object(ReflectionParameter))中的Container.php第817行
  • 在Container-> getDependencies(array(object(ReflectionParameter)),array())在Container.php第790行

1 个答案:

答案 0 :(得分:1)

所有步骤都完美无缺,但您需要在config/app.php数组providers下的'providers' => [ // .. others WechatServiceProvider::class, ], 注册您的服务提供商:

sun.awt.image.codec.*