【问题标题】:when I built my first dependency injected class in Laravel 5.3 I got a BindingResolutionException当我在 Laravel 5.3 中构建我的第一个依赖注入类时,我得到了一个 BindingResolutionException
【发布时间】:2017-05-12 11:42:31
【问题描述】:

这是我的代码:

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 类中无法解析的依赖解析 [Parameter #0 [ $options ]]
  • 在 Container.php 第 850 行
  • 在 Container->resolveNonClass(object(ReflectionParameter)) 在 Container.php 第 817 行
  • 在 Container->getDependencies(array(object(ReflectionParameter)), array()) in Container.php line 790

【问题讨论】:

  • 我认为你需要在这里运行composer dump-autoload

标签: php laravel dependency-injection laravel-5.3


【解决方案1】:

所有步骤都已完美完成,但是您还需要在config/app.php 中的providers 数组下注册您的服务提供者:

'providers' => [
    // .. others
    WechatServiceProvider::class,
],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2013-02-21
    • 1970-01-01
    相关资源
    最近更新 更多