Slim Framework PHP-DI container integration

2.1 2018-10-20 16:45 UTC


PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

Slim Framework PHP-DI container integration

PHP-DI (v6) dependency injection container integration for Slim Framework.

In order to allow possible services out there expecting the container to be Slim\Container (extending Pimple) and thus implementing ArrayAccess, it has been added to default provided container.

You are encouraged to use array syntax for assignment instead of PHP-DI set method if you plan to reuse your code with default Slim container.


Best way to install is using Composer:

composer require juliangut/slim-php-di

Then require_once the autoload file:

require_once './vendor/autoload.php';


Use \Jgut\Slim\PHPDI\App which will build PHP-DI container.

use Interop\Container\ContainerInterface;
use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\App;

$settings = require __DIR__ . '/settings.php';
$configuration = new Configuration($settings);

$app = new App($configuration);
$container = $app->getContainer();

// Register services the PHP-DI way
$container->set('service_one', function (ContainerInterface $container) {
    return new ServiceOne($container->get('service_two'));

// \Jgut\Slim\PHPDI\Container accepts registering services à la Pimple
$container['service_two'] =  function (ContainerInterface $container) {
    return new ServiceTwo();

// Set your routes



Or build container and provide it to default Slim App.

use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\ContainerBuilder;
use Slim\App

$settings = require __DIR__ . '/settings.php';
$container = ContainerBuilder::build(new Configuration($settings));

// ...

$app = new App($container);

// ...



use Jgut\Slim\PHPDI\Configuration;

$settings = [
    'useAnnotations' => true,
    'ignorePhpDocErrors' => true,
$configuration = new Configuration($settings);

// Can be set after creation

PHP-DI settings

  • useAutoWiring, whether or not to use auto wiring (true by default)
  • useAnnotations, whether or not to use annotations (false by default)
  • useDefinitionCache, whether or not to use definition cache (false by default)
  • ignorePhpDocErrors, whether or not to ignore phpDoc errors on annotations (false by default)
  • wrapContainer, wrapping container (none by default)
  • proxiesPath, path where PHP-DI creates its proxy files (none by default)
  • compilationPath, path to where PHP-DI creates its compiled container (none by default)

Refer to PHP-DI documentation to learn more about container configurations.

In order for you to use annotations you have to require doctrine/annotations. See here

Additional settings

  • containerClass, container class that will be built. Must implement \Interop\Container\ContainerInterface, \DI\FactoryInterface and \DI\InvokerInterface (\Jgut\Slim\PHPDI\Container by default)
  • definitions, an array of paths to definition files/directories or arrays of definitions. Definitions are loaded in order of appearance

Services registration order

Services are registered in the following order:

  • Default Slim services
  • Definitions provided in configuration in the order they are in the array

Array value access shorthand

Default \Jgut\Slim\PHPDI\Container container allows shorthand to access array values by concatenating array keys with dots. If any key in the chain is not defined normal container's ContainerValueNotFoundException is thrown

$container->get('settings')['displayErrorDetails']; // true
$container->get('settings.displayErrorDetails'); // true

This functionality would most commonly be used to directly access settings but can be used to access any other array defined in the container


Be careful though not to shadow any array keys by using dots in keys

$settings = [
    'foo' => [
        'bar' => [
            'baz' => 'shadowed!',
    'foo.bar' => 'bang!',
$container->set('settings', $settings);

$container->get('settings.foo.bar'); // bang!
$container->get('settings.foo.bar.baz'); // ContainerValueNotFoundException thrown

The easiest way to avoid this from ever happening is by not using dots in keys

Migration from 1.x

  • Minimum Slim version is now 3.9
  • PHP-DI have been upgraded to v6. Review PHP-DI documentation: container compilation, create/autowire functions, etc
  • PHP-DI settings have been moved into Configuration object. This object accepts an array of settings on instantiation so it's just a matter of providing the settings to it
  • Configuration settings names have changed from snake_case to camelCase
  • Definitions are included in Configuration object rather than set apart. Now you can as well define path(s) to load definition files from


Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.



See file LICENSE included with the source code for a copy of the license terms.