IoC Container

Introduction

The Laravel inversion of control container is a powerful tool for managing class dependencies. Dependency injection is a method of removing hard-coded class dependencies. Instead, the dependencies are injected at run-time, allowing for greater flexibility as dependency implementations may be swapped easily.

Understanding the Laravel IoC container is essential to building a powerful, large application, as well as for contributing to the Laravel core itself.

Basic Usage

Binding A Type Into The Container

There are two ways the IoC container can resolve dependencies: via Closure callbacks or automatic resolution. First, we'll explore Closure callbacks. First, a "type" may be bound into the container:

  1. App::bind('foo', function($app)
  2. {
  3. return new FooBar;
  4. });

Resolving A Type From The Container

  1. $value = App::make('foo');

When the App::make method is called, the Closure callback is executed and the result is returned.

Binding A "Shared" Type Into The Container

Sometimes, you may wish to bind something into the container that should only be resolved once, and the same instance should be returned on subsequent calls into the container:

  1. App::singleton('foo', function()
  2. {
  3. return new FooBar;
  4. });

Binding An Existing Instance Into The Container

You may also bind an existing object instance into the container using the instance method:

  1. $foo = new Foo;
  2. App::instance('foo', $foo);

Where To Register Bindings

IoC bindings, like event handlers or route filters, generally fall under the title of "bootstrap code". In other words, they prepare your application to actually handle requests, and usually need to be executed before a route or controller is actually called. Like most other bootstrap code, the start files are always an option for registering IoC bindings. Alternatively, you could create an app/ioc.php (filename does not matter) file and require that file from your start file.

If your application has a very large number of IoC bindings, or you simply wish to organize your IoC bindings in separate files by category, you may register your bindings in a service provider.

Automatic Resolution

Resolving A Class

The IoC container is powerful enough to resolve classes without any configuration at all in many scenarios. For example:

  1. class FooBar {
  2. public function __construct(Baz $baz)
  3. {
  4. $this->baz = $baz;
  5. }
  6. }
  7. $fooBar = App::make('FooBar');

Note that even though we did not register the FooBar class in the container, the container will still be able to resolve the class, even injecting the Baz dependency automatically!

When a type is not bound in the container, it will use PHP's Reflection facilities to inspect the class and read the constructor's type-hints. Using this information, the container can automatically build an instance of the class.

Binding An Interface To An Implementation

However, in some cases, a class may depend on an interface implementation, not a "concrete type". When this is the case, the App::bind method must be used to inform the container which interface implementation to inject:

  1. App::bind('UserRepositoryInterface', 'DbUserRepository');

Now consider the following controller:

  1. class UserController extends BaseController {
  2. public function __construct(UserRepositoryInterface $users)
  3. {
  4. $this->users = $users;
  5. }
  6. }

Since we have bound the UserRepositoryInterface to a concrete type, the DbUserRepository will automatically be injected into this controller when it is created.

Practical Usage

Laravel provides several opportunities to use the IoC container to increase the flexibility and testability of your application. One primary example is when resolving controllers. All controllers are resolved through the IoC container, meaning you can type-hint dependencies in a controller constructor, and they will automatically be injected.

Type-Hinting Controller Dependencies

  1. class OrderController extends BaseController {
  2. public function __construct(OrderRepository $orders)
  3. {
  4. $this->orders = $orders;
  5. }
  6. public function getIndex()
  7. {
  8. $all = $this->orders->all();
  9. return View::make('orders', compact('all'));
  10. }
  11. }

In this example, the OrderRepository class will automatically be injected into the controller. This means that when unit testing a "mock" OrderRepository may be bound into the container and injected into the controller, allowing for painless stubbing of database layer interaction.

Other Examples Of IoC Usage

Filters, composers, and event handlers may also be resolved out of the IoC container. When registering them, simply give the name of the class that should be used:

  1. Route::filter('foo', 'FooFilter');
  2. View::composer('foo', 'FooComposer');
  3. Event::listen('foo', 'FooHandler');

Service Providers

Service providers are a great way to group related IoC registrations in a single location. Think of them as a way to bootstrap components in your application. Within a service provider, you might register a custom authentication driver, register your application's repository classes with the IoC container, or even setup a custom Artisan command.

In fact, most of the core Laravel components include service providers. All of the registered service providers for your application are listed in the providers array of the app/config/app.php configuration file.

Defining A Service Provider

To create a service provider, simply extend the Illuminate\Support\ServiceProvider class and define a register method:

  1. use Illuminate\Support\ServiceProvider;
  2. class FooServiceProvider extends ServiceProvider {
  3. public function register()
  4. {
  5. $this->app->bind('foo', function()
  6. {
  7. return new Foo;
  8. });
  9. }
  10. }

Note that in the register method, the application IoC container is available to you via the $this->app property. Once you have created a provider and are ready to register it with your application, simply add it to the providers array in your app configuration file.

Registering A Service Provider At Run-Time

You may also register a service provider at run-time using the App::register method:

  1. App::register('FooServiceProvider');

Container Events

Registering A Resolving Listener

The container fires an event each time it resolves an object. You may listen to this event using the resolving method:

  1. App::resolvingAny(function($object)
  2. {
  3. //
  4. });
  5. App::resolving('foo', function($foo)
  6. {
  7. //
  8. });

Note that the object that was resolved will be passed to the callback.