SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
Dependency injection and inversion of control are terms often used when we want to decouple code and write testable code. We take a look at how Laravel's IoC container and how it works to apply this to our code. It also covers how it's used for Laravel's internal components and what kind of awesome features provided by the framework that are available to us, such as service providers and interface bindings.
Dependency injection and inversion of control are terms often used when we want to decouple code and write testable code. We take a look at how Laravel's IoC container and how it works to apply this to our code. It also covers how it's used for Laravel's internal components and what kind of awesome features provided by the framework that are available to us, such as service providers and interface bindings.
2.
Me, myself& I
Niklas Modess
Web architect at Nevertheless
@niklasmodess
http://www.codingswag.com
3.
Deploying PHP
Applications
» Currently writing
» Deploy process for PHP
applications
» https://leanpub.com/
deploying-php-applications
4.
Symfony
November Camp
» Full day of PHP/Symfony @
Scandic Grand Central
» 14th of November
» I will give a talk on
"Deploying PHP
applications"
» http://www.symfony.se/
november-camp/
5.
The topic ofthistalk
is kind ofalie...“Laravels IoC container”
6.
Thetruth...
The Laravelapplication
isan IoC container
class Application extends Container [...] {
[...]
}
9.
Instantiation
App::bind('foo', function()
{
return new FooBar;
});
$foo = App::make('foo'); // instance of FooBar
Or resolvethe class directly
$foo = App::make('FooBar'); // same as 'new FooBar';
10.
Singleton objects
App::singleton('bar', function()
{
return new BarFoo;
});
$foo = App::make('bar'); // instance of BarFoo
11.
Swapoutbinded objects
$foo = new Foo;
App::instance('foo', $foo);
» Great for testing
14.
Automatic dependencyresolving
class OrderController extends BaseController {
protected $orders;
public function __construct(OrderRepository $orders)
{
$this->orders = $orders;
}
public function getIndex()
{
$all = $this->orders->all();
return View::make('orders', compact('all'));
}
}
15.
Instead of
$orderRepository = new OrderRepository;
$orderController = new OrderController($orderRepository);
We use
$orderController = App::make('OrderController');
Couldalso beachievedthisway
$orderRepository = new OrderRepository;
$orderController = App::make('OrderController', [$orderRepository]);
16.
More awesome stuff
* Service providers
* Interface binding