dimanche 28 février 2016

How to access Laravel Singletons in a class?

I have registered a singleton in a service provider like so:

$this->app->singleton(MyClass::class);

This can normally be accessed by simply stating it in the parameters like so:

class FooController extends Controller
{
    public function index(MyClass $myClass)
    {
        //...
    }
}

However I cannot seem to be able to access this singleton in other custom classes (i.e. non controller classes). (http://ift.tt/1KUSqBz)

For example like here:

class Bar {
   private $myClass;

   public function __constructe() {
      $this->myClass = ... // TODO: get singleton
   }
}

How do I do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire