Dependency Container

Slim uses a dependency container to prepare, manage, and inject applicationdependencies. Slim supports containers that implement PSR-11 or the Container-Interop interface. You can use Slim’s built-in container (based on Pimple)or third-party containers like Acclimateor PHP-DI.

How to use the container

You don’t have to provide a dependency container. If you do, however, you mustinject the container instance into the Slim application’s constructor.

  1. $container = new \Slim\Container;
  2. $app = new \Slim\App($container);

Add a service to Slim container:

  1. $container = $app->getContainer();
  2. $container['myService'] = function ($container) {
  3. $myService = new MyService();
  4. return $myService;
  5. };

You can fetch services from your container explicitly or implicitly.You can fetch an explicit reference to the container instance from inside a Slimapplication route like this:

  1. /**
  2. * Example GET route
  3. *
  4. * @param \Psr\Http\Message\ServerRequestInterface $req PSR7 request
  5. * @param \Psr\Http\Message\ResponseInterface $res PSR7 response
  6. * @param array $args Route parameters
  7. *
  8. * @return \Psr\Http\Message\ResponseInterface
  9. */
  10. $app->get('/foo', function ($req, $res, $args) {
  11. $myService = $this->get('myService');
  12. return $res;
  13. });

You can implicitly fetch services from the container like this:

  1. /**
  2. * Example GET route
  3. *
  4. * @param \Psr\Http\Message\ServerRequestInterface $req PSR7 request
  5. * @param \Psr\Http\Message\ResponseInterface $res PSR7 response
  6. * @param array $args Route parameters
  7. *
  8. * @return \Psr\Http\Message\ResponseInterface
  9. */
  10. $app->get('/foo', function ($req, $res, $args) {
  11. $myService = $this->myService;
  12. return $res;
  13. });

To test if a service exists in the container before using it, use the has() method, like this:

  1. /**
  2. * Example GET route
  3. *
  4. * @param \Psr\Http\Message\ServerRequestInterface $req PSR7 request
  5. * @param \Psr\Http\Message\ResponseInterface $res PSR7 response
  6. * @param array $args Route parameters
  7. *
  8. * @return \Psr\Http\Message\ResponseInterface
  9. */
  10. $app->get('/foo', function ($req, $res, $args) {
  11. if($this->has('myService')) {
  12. $myService = $this->myService;
  13. }
  14. return $res;
  15. });

Slim uses get() and isset() magic methods that defer to the application’scontainer for all properties that do not already exist on the application instance.

Required services

Your container MUST implement these required services. If you use Slim’s built-in container, these are provided for you. If you choose a third-party container, you must define these required services on your own.

  • settings
  • Associative array of application settings, including keys:
    • httpVersion
    • responseChunkSize
    • outputBuffering
    • determineRouteBeforeAppMiddleware.
    • displayErrorDetails.
    • addContentLengthHeader.
    • routerCacheFile.
  • environment
  • Instance of \Slim\Http\Environment.
  • request
  • Instance of \Psr\Http\Message\ServerRequestInterface.
  • response
  • Instance of \Psr\Http\Message\ResponseInterface.
  • router
  • Instance of \Slim\Interfaces\RouterInterface.
  • foundHandler
  • Instance of \Slim\Interfaces\InvocationStrategyInterface.
  • phpErrorHandler
  • Callable invoked if a PHP 7 Error is thrown. The callable MUST return an instance of \Psr\Http\Message\ResponseInterface and accept three arguments:
  • \Psr\Http\Message\ServerRequestInterface
  • \Psr\Http\Message\ResponseInterface
  • \Error
  • errorHandler
  • Callable invoked if an Exception is thrown. The callable MUST return an instance of \Psr\Http\Message\ResponseInterface and accept three arguments:
  • \Psr\Http\Message\ServerRequestInterface
  • \Psr\Http\Message\ResponseInterface
  • \Exception
  • notFoundHandler
  • Callable invoked if the current HTTP request URI does not match an application route. The callable MUST return an instance of \Psr\Http\Message\ResponseInterface and accept two arguments:
  • \Psr\Http\Message\ServerRequestInterface
  • \Psr\Http\Message\ResponseInterface
  • notAllowedHandler
  • Callable invoked if an application route matches the current HTTP request path but not its method. The callable MUST return an instance of \Psr\Http\Message\ResponseInterface and accept three arguments:
  • \Psr\Http\Message\ServerRequestInterface
  • \Psr\Http\Message\ResponseInterface
  • Array of allowed HTTP methods
  • callableResolver
  • Instance of \Slim\Interfaces\CallableResolverInterface.