Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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-...
Symfony
November Camp
» Full day of PHP/Symfony @
Scandic Grand Central
» 14th of November
» I will give a talk on
"Deploy...
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 resol...
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 __construc...
Instead of
$orderRepository = new OrderRepository;
$orderController = new OrderController($orderRepository);
We use
$order...
More awesome stuff
* Service providers
* Interface binding
Questions?
Upcoming SlideShare
Loading in …5
×

Laravels IoC Container

350 views

Published on

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.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Laravels IoC Container

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

×