Tutorial by Examples

In a Service Provider register method we can bind an interface to an implementation: public function register() { App::bind( UserRepositoryInterface::class, EloquentUserRepository::class ); } From now on, everytime the app will need an instance of UserRepositoryInterface, Larave...
We can use the Service Container as a Registry by binding an instance of an object in it and get it back when we'll need it: // Create an instance. $john = new User('John'); // Bind it to the service container. App::instance('the-user', $john); // ...somewhere and/or in another class... ...
We can bind a class as a Singleton: public function register() { App::singleton('my-database', function() { return new Database(); }); } This way, the first time an instance of 'my-database' will be requested to the service container, a new instance will be created. Al...
The Service Container is the main Application object. It can be used as a Dependency Injection Container, and a Registry for the application by defining bindings in the Service Providers Service Providers are classes where we define the way our service classes will be created through the applicatio...
We can use the Service Container as a Dependency Injection Container by binding the creation process of objects with their dependencies in one point of the application Let's suppose that the creation of a PdfCreator needs two objects as dependencies; every time we need to build an instance of PdfCr...

Page 1 of 1