绕过Laravel服务提供商


Laravel Service Provider bypassed

我有以下类:

<?php
namespace App'CustomClasses;
class Disqus {
    protected $secretKey;
    protected $publicKey;
    public function __construct()
    {
        $this->secretKey = 'abc';
        $this->publicKey = '123';
    }
    public function payload()
    {
    ...
    }    
}

我还创建了一个服务提供者(如下所示),将这个类绑定到IOC容器:

<?php namespace App'Providers;
use Illuminate'Support'ServiceProvider;
use App'CustomClasses'Disqus;
class DisqusServiceProvider extends ServiceProvider {
    public function register()
    {
        $this->app->singleton('Disqus', function() {
            return new Disqus();
        });
    }
    public function boot()
    {
        //
    }
}

在我的控制器中:

<?php
use App'CustomClasses'Disqus;
class ArticlesController extends Controller {
    public function view(Disqus $disqus)
    {
    ...
    //$disqus = App::make('Disqus');
    return View::make('articles.view', compact(array('disqus')));
    }
}

问题是,每当我使用$disqus变量时,它不是从服务提供者"生成"的,而是从Disqus类本身生成的。

然而,当我有$disqus = App::make('Disqus');时,变量通过服务提供者。

所以我的问题是,既然绑定存在于服务提供者,不应该$disqus变量来自DisqusServiceProvider而不是Disqus类直接当我在我的控制器中使用它?

我错过了什么吗?

提前感谢您的帮助。

当控制器的动作需要传递类App'CustomClasses'Disqus的对象时,服务容器会在它的映射中搜索依赖的类名,看看它是否有相应的服务。但是,它使用了完全限定的类名,这就是它在您的情况下不能正确工作的原因。

在你的服务提供商中,你已经将服务绑定到Disqus,而完全限定类名称是App'CustomClasses'Disqus。在提供程序中使用完全限定类名,它应该可以工作。