Dependency Container
Slim uses a dependency container to prepare, manage, and inject application dependencies. Slim supports containers that implement the Container-Interop interface. You can use Slim’s built-in container (based on Pimple) or third-party containers like Acclimate or PHP-DI.
How to use the container
You don’t have to provide a dependency container. If you do, however, you must inject the container instance into the Slim application’s constructor.
$container = new SlimContainer;
$app = new SlimApp($container);
You can fetch services from your container explicitly or implicitly. You can fetch an explicit reference to the container instance from inside a Slim application route like this:
/**
* Example GET route
*
* @param PsrHttpMessageServerRequestInterface $req PSR7 request
* @param PsrHttpMessageResponseInterface $res PSR7 response
* @param array $args Route parameters
*
* @return PsrHttpMessageResponseInterface
*/
$app->get("/foo", function ($req, $res, $args) {
$container = $this->getContainer();
$myService = $container->get("myService");
return $res;
});
You can implicitly fetch services from the container like this:
/**
* Example GET route
*
* @param PsrHttpMessageServerRequestInterface $req PSR7 request
* @param PsrHttpMessageResponseInterface $res PSR7 response
* @param array $args Route parameters
*
* @return PsrHttpMessageResponseInterface
*/
$app->get("/foo", function ($req, $res, $args) {
$myService = $this->myService;
return $res;
});
Slim uses __get()
and __isset()
magic methods that defer to the application’s container 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
, outputBuffering
,responseChunkSize
and determineRouteBeforeAppMiddleware
.
environment
Instance of SlimInterfacesHttpEnvironmentInterface
.
request
Instance of PsrHttpMessageServerRequestInterface
.
response
Instance of PsrHttpMessageResponseInterface
.
router
Instance of SlimInterfacesRouterInterface
.
foundHandler
Instance of SlimInterfacesInvocationStrategyInterface
.
errorHandler
Callable invoked if application error. The callable MUST return an instance ofPsrHttpMessageResponseInterface
and accept three arguments:
PsrHttpMessageServerRequestInterface
PsrHttpMessageResponseInterface
Exception
notFoundHandler
Callable invoked if the current HTTP request URI does not match an application route. The callable MUST return an instance of PsrHttpMessageResponseInterface
and accept two arguments:
PsrHttpMessageServerRequestInterface
PsrHttpMessageResponseInterface
notAllowedHandler
Callable invoked if an application route matches the current HTTP request path but not its method. The callable MUST return an instance of PsrHttpMessageResponseInterface
and accept three arguments:
PsrHttpMessageServerRequestInterface
PsrHttpMessageResponseInterface
- Array of allowed HTTP methods