Successfully reported this slideshow.

Laravels IoC Container

0

Share

Loading in …3
×
1 of 17
1 of 17

Laravels IoC Container

0

Share

Download to read offline

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.

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Related Audiobooks

Free with a 14 day trial from Scribd

See all

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?

×