gorgi93
gorgi93

Reputation: 2535

Laravel 4 service provider instance

I am trying do to facade with laravel 4. I am using service provider. In the service provider register I make singleton instance but I want to access it through interface. It is working I just do not know how to access the interface with the reference to that object. My code is bellow:

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

}

And then if I write this in another php class:

Auth\iAuthenticate::someMethod()

It returns that I cannot call abstract methods which doesn't supprise me. How can I access instance that was created within service provider?

Upvotes: 0

Views: 1018

Answers (1)

user1669496
user1669496

Reputation: 33148

You can use $instance = App::make('Auth\iAuthenticate');

It's also suggested to use the this->app property. It might save you from some headaches in the future.

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

Upvotes: 1

Related Questions