Laravels
IoC
container
Me, myself& I
Niklas Modess
Web architect at Nevertheless
@niklasmodess
http://www.codingswag.com
Deploying PHP
Applications
» Currently writing
» Deploy process for PHP
applications
» https://leanpub.com/
deploying-php-applications
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/
The topic ofthistalk
is kind ofalie...“Laravels IoC container”
Thetruth...
The Laravelapplication
isan IoC container
class Application extends Container [...] {
[...]
}
Dependencyinjection
comes first
App::make('foo');
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';
Singleton objects
App::singleton('bar', function()
{
return new BarFoo;
});
$foo = App::make('bar'); // instance of BarFoo
Swapoutbinded objects
$foo = new Foo;
App::instance('foo', $foo);
» Great for testing
Internal
dependencies
Taylor magic™
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'));
}
}
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]);
More awesome stuff
* Service providers
* Interface binding
Questions?

Laravels IoC Container