Symfony 2 DIC integration for the Slim Framework 3

1.1.5 2018-01-06 06:22 UTC


Quality Assurance

PHP 5.5 PHP 5.6 PHP 7 Build Status Code Climate Test Coverage

The above badges represent the current development branch. As a rule, I don't push to GitHub unless tests, coverage and usability are acceptable. This may not be true for short periods of time; on holiday, need code for some other downstream project etc. If you need stable code, use a tagged version. Read 'Installation'.

See the tests and the Test Contract for additional information.

End of life notice

In March 2018, developer support will be withdrawn from this library for PHP <5.6. Older versions of PHP are now in such little use that the added effort of maintaining compatibility is not effort effective. See PHP Version Stats for the numbers.


Provides a Symfony Dependency Injection Container V2 for a Slim Application V3


The Slim framework is great for lightweight sites and in version V3 adopts the interop interfaces for dependency injection containers. Slim V3 uses the Pimple DI by default. Symfony DI does not yet support the interop interface definition.

This small library supports the integration of the easy to use, yet powerful Symfony version of a DI container with the lightweight Slim Framework, giving you the ability to create great, maintainable and configurable web sites quickly.

The Builder supports XML DI definition. XML is the most powerful and complete form of Symfony DI configuration. The Builder also supports Yaml DI definition. Many developers prefer to put the parameters into a Yaml file and service definitions into an XML file. The supplied minimal example files demonstrate this usage.


To create and return a Slim\App object with a Symfony DIC:

use Chippyash\Type\String\StringType;
use Slimdic\Dic\Builder;
use Slim\App;

$xmlDiFileLocation = '/mysite/cfg/dic.production.xml';

 * @var Slim\App
$app = new App(Builder::buildDic(new StringType($xmlDiFileLocation)));

Please see the examples/dic.slim.xml and dic.slim.yml files for the minimum that you need to build the DIC with to support Slim. You are recommended to put the files in with the rest of your DI configs and use the <imports> directive in your main config to pull it in.

You can add to the compilation process by utilising the pre and post compile functions. This is often useful for setting up synthetic services or initialising parameters in the DI container.

Register a PreCompile function

The PreCompile function is called just before the container is compiled.

use Slimdic\Dic\ServiceContainer;
use Symfony\Component\DependencyInjection\Definition;

Builder::registerPreCompileFunction(function(ServiceContainer $dic) {
    //set a parameter
    $dic->setParameter('foo', 'bar');
    //set up a synthetic
    $dic->setDefinition('bar', (new Definition())->setSynthetic(true));

$app = new App(Builder::buildDic(new StringType($xmlDiFileLocation)));

Register a PostCompile function

The PostCompile function is called just after compiling the container.

The post compile function only really makes sense to set a synthetic definition as after compilation the rest of the DI Container is frozen and cannot be changed.

use Slimdic\Dic\ServiceContainer;

Builder::registerPreCompileFunction(function(ServiceContainer $dic) {
    $dic->setDefinition('foo', (new Definition())->setSynthetic(true));

Builder::registerPostCompileFunction(function(ServiceContainer $dic, $stage) {
    $dic->set('foo', $myFooService);

Changing the library

  1. fork it
  2. write the test
  3. amend it
  4. do a pull request

Found a bug you can't figure out?

  1. fork it
  2. write the test
  3. do a pull request

or raise an issue ticket

NB. Make sure you rebase to HEAD before your pull request


The library is hosted at Github. It is available at

Check out ZF4 Packages for more packages

See My blog for ramblings on coding and curries


Install Composer

For production


    "chippyash/slim-symfony-dic": "~1"

to your composer.json "requires" section

For development

Clone this repo, and then run Composer in local repo root to pull in dependencies

    git clone Slimdic
    cd Slimdic
    composer install --dev

To run the tests:

    cd Slimdic
    vendor/bin/phpunit -c test/phpunit.xml test/


This software library is released under the GNU GPL V3 or later license

This software library is Copyright (c) 2016, Ashley Kitson, UK

A commercial license is available for this software library, please contact the author. It is normally free to deserving causes, but gets you around the limitation of the GPL license, which does not allow unrestricted inclusion of this code in commercial works.


V1.0.0 Initial Release

V1.1.0 Update dependencies

V1.1.1 Add link to packages

V1.1.2 Verify PHP 7 compatibilty

V1.1.3 Update Slim dependency to 3.*

V1.1.4 Update dependencies

V1.1.5 Update build script