【问题标题】:Laravel 4 service provider instanceLaravel 4 服务提供者实例
【发布时间】:2014-04-20 03:40:25
【问题描述】:

我正在尝试使用 laravel 4 进行外观。我正在使用服务提供商。在服务提供者寄存器中,我创建了单例实例,但我想通过接口访问它。它正在工作我只是不知道如何通过对该对象的引用来访问接口。我的代码如下:

class AuthenticationServiceProvider extends ServiceProvider
{
    public function register()
    {
         \App::singleton('Auth\iAuthenticate', function()
         {
            return new DbAuthentication();
         });            
    }

}

然后如果我在另一个 php 类中写这个:

Auth\iAuthenticate::someMethod()

它返回我不能调用不让我惊讶的抽象方法。如何访问在服务提供者中创建的实例?

【问题讨论】:

  • 你试过$instance = App::make('Auth\iAuthenticate');吗?
  • 不是真的......这实际上是答案。 :)
  • 好的,我已经添加了答案。

标签: php laravel laravel-4


【解决方案1】:

您可以使用$instance = App::make('Auth\iAuthenticate');

还建议使用this->app 属性。它可能会让您在未来免于一些麻烦。

public function register()
{
     $this->app->singleton('Auth\iAuthenticate', function()
     {
        return new DbAuthentication();
     });            
} 

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 2015-10-15
    • 1970-01-01
    • 2016-12-21
    • 2015-10-24
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    相关资源
    最近更新 更多