Laravel Classroom image

Prashant  Nigam / Student / Web Technology

To post your Question Join Classroom
Lesson Topics's No:-  |1 | 2 | 3|Last
Lessons:-The IoC Container

The IoC Container
The IoC container is a tricky subject, many people are confused by its description in the
documentation, and for a short time I was included in those people. A great deal of research,
and the support of the fantastic Laravel community (join us in #laravel on freenode IRC) has
cleared up the topic nicely. Hopefully I will be able to shed some light on this mysterious topic.
IoC stands for Inversion of Control, I don’t want to complicate things with a full description,
there are many online articles which will cover the nerdier side of this topic. Instead think of the
container as ‘Inverting the Control’ or ‘Handing control back to Laravel’ to resolve our objects.
That’s what the container is all about, resolving objects. Other than its use for injecting
dependencies for use in unit tests (we will cover this later) you can simply think of the IoC
container as a ‘shortcut’ for resolving complex objects, or following a singleton pattern, without
the usual class associated with the pattern. More on singletons later, let’s have a look at
registering a objects with the container.
20.1 Registering Objects
Let’s use our imaginations, like the big purple dinosaur on the TV taught us. We will be imagining
a class called ‘Discoball’ which will be used all over our application for various groovy purposes.
Unfortunately, our Discoball class requires a lot of configuration before it can be used, let’s have
a look at that.
1 <?php
2 $db = new Discoball(Discoball::SHINY);
3 $db->configure_shinyness('max');
4 $db->spin_speed('8900rpm');
Woah! That’s a lot of settings. Now it would soon get boring to have to instantiate and setup
our discoball every time we want to use it. Let’s let the IoC container instantiate it for us, and
jump right in with a code sample.
I like to put this code into start.php, but you can put it anywhere you like, as long as your objects
are registered before you try to resolve them.
1 <?php
3 // application/start.php
5 IoC::register('discoball', function() {
7 // instantiate our object as before
8 $db = new Discoball(Discoball::SHINY);
9 $db->configure_shinyness('max');10 $db->spin_speed('8900rpm');
12 // hand the object as the result of the closure
13 return $db;
14 });
We use the IoC::register() method to register our object with the controller. The first
parameter is a string that will be used to resolve the object later, I used the word ‘discoball’
as it made the most sense to me. The second parameter is a closure that we can use to instantiate
our object.
Inside the closure you will see the familiar discoball configuration code, and we will return the
configured discoball object from the closure.
Great! Our object is registered, and that’s all there is to the Io… just kidding. Let’s have a look
at how we can use our registered object.


Prashant  Nigam

Skills    Laravel

Qualifications :-
Location :-,,,

  Students (0)