container.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. return array(
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Inversion of Control Container
  6. |--------------------------------------------------------------------------
  7. |
  8. | Here you may define resolvers for the Laravel inversion of control (IoC)
  9. | container. An IoC container provides the ability to create more flexible
  10. | and testable applications, as well as a convenient method of managing
  11. | the instantiation of complex objects.
  12. |
  13. | To register a resolver in the container, simple create add an item to
  14. | the array for the object with a closure that returns an instance of
  15. | the object.
  16. |
  17. | For example, here's how to register a resolver for a Mailer class:
  18. |
  19. | 'mailer' => function($c)
  20. | {
  21. | return new Mailer($sender, $key);
  22. | }
  23. |
  24. | Note that the container instance itself is passed into the resolver,
  25. | allowing you to continue to resolve dependencies within the resolver
  26. | itself. This allows you to easily resolve nested dependencies.
  27. |
  28. | When creating controller instances, Laravel will check to see if a
  29. | resolver has been registered for the controller. If it has, it will
  30. | be used to create the controller instance. All controller resolvers
  31. | should be registered beginning using a {controllers}.{name} naming
  32. | convention. For example:
  33. |
  34. | 'controllers.user' => function($c)
  35. | {
  36. | return new User_Controller($c->resolve('repository'));
  37. | }
  38. |
  39. | Of course, sometimes you may wish to register an object as a singleton
  40. | Singletons are resolved by the controller the first time they are
  41. | resolved; however, that same resolved instance will continue to be
  42. | returned by the container each time it is requested. Registering an
  43. | object as a singleton couldn't be simpler:
  44. |
  45. | 'mailer' => array('singleton' => true, 'resolver' => function($c)
  46. | {
  47. | return new Mailer($sender, $key);
  48. | })
  49. |
  50. */
  51. );