簡體   English   中英

Laravel 4服務提供商實例

[英]Laravel 4 service provider instance

我正在嘗試與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');

還建議使用this->app屬性。 它可以使您免於以后的麻煩。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM