Introduces the Symfony 2 DI container in a Symfony 1 application.

3.1.1 2016-11-22 14:34 UTC


  • Introduces the Symfony 2 DI container in a Symfony 1 application.
  • Transforms a Symfony 2 doctrine configuration into a format readable for Symfony 1.x. Use Doctrine entities and propel classes with the same database configuration!

Total Downloads Build Status


  1. Download Composer.
  2. Add to your composer.json
"require": {
    "hostnet/hn-dependency-injection-plugin": "1.0.*"

  1. Run php composer.phar install.
  2. Make apps/<app>/config/<app>Configuration extend Hostnet\HnDependencyInjectionPlugin\ApplicationConfiguration.
  3. [Optional] override the getKernel method to return your own kernel, registering the bundles you want.
    protected function createKernel()
        return new MyKernel($this);
    class MyKernel extends Hostnet\HnDependencyInjectionPlugin\Symfony1Kernel
        public function registerBundles()
            $bundles = array(
                new Symfony\Bundle\FrameworkBundle\FrameworkBundle()
            return array_merge($bundles, parent::registerBundles());
  4. Create apps/<app>/config/config.yml to configure your Doctrine dbal, Doctrine orm, and possibly the FrameworkBundle. See also the example configuration.
  5. If you don't want to generate the propel backwards compatability layer, add this
        hn_entities_enable_backwards_compatible_connections: false
    to config.yml, or parameters.yml if you prefer to have them separate.
  6. To ensure proper autoloading when using Doctrine entities, remove if you have
    require_once __DIR__ . '/../vendor/autoload.php';
    and add the following to your config/ProjectConfiguration.php.
    use Doctrine\Common\Annotations\AnnotationRegistry;
    $loader = require __DIR__.'/../vendor/autoload.php';
    AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
    That way Doctrine knows where to find your entities.
  7. Be sure to set up your permissions properly, see "Setting up your permissions".
  8. After this is done we can do a little cleanup. To prevent confusion you should remove config/databases.yml, since only the Symfony 2 configuration is read at this point.



  • Removed the Symfony1Panel class, it's added automatically now.


  • Added a web debug panel with a link to the Symfony 2 profiler.

For this you need to activate the WebProfilerBundle, which you should only activate in dev

if ($this->getEnvironment() == 'dev') {
    $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();

Then you need to configure the WebProfilerBundle and the profiler; add this to your config_dev.yml:

    toolbar: true
    router:   { resource: "%kernel.root_dir%/../config/sf2_routing_dev.yml" }
        only_exceptions: false
        only_master_requests: true

Add this to sf2_routing_dev.yml to make the WebProfilerBundle accessible:

    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

    resource: sf2_routing.yml

And in web/*.php, replace $configuration->handle($request)->send(); with:

$response = $configuration->handle($request);
$configuration->terminate($request, $response);

You should now have a new panel in the Symfony 1 web debug toolbar with a link to the Symfony 2 profiler!


  • First official release


  • Added hn_entities_enable_backwards_compatible_connections parameter

Running the unit-tests

  1. Clone the repository yourself
  2. Go to the directory of the clone
  3. Run composer.phar install
  4. Run phpunit

Moving the error handling to Symfony 2

When migrating, you will eventually hit a point where you want to log errors properly. By default the sfFrontWebController contains a try catch block where exceptions will be caught. In order to move all error handling to Symfony 2, you can create your own front controller. If you remove the try catch in this front controller, all errors will be caught by the Symfony 2 uncaught exception handler.

If your route is matched with the sf1 route but sf1 doesn't know about the route, it will throw an sf404ErrorException. This exception will be caught and wrapped into an HttpNotFoundException and the kernel.exception will be fired eventually. This will make sure you can handle all exceptions in Symfony 2.

Additionally, you can throw exceptions in sf1 such as AccessDeniedException, NotFoundHttpException, \RuntimeException etc. They will all be caught. In case of a 404 error, the sf1 fallback will only be triggered once and can only be triggered as a first controller. Once you've succesfully entered Symfony 2 via either the sf1 route or the normal entry, it will no longer be initiated.

To add the front controller, you will have to set it in factories.yml.

    class: MyCustomFrontController

To enable the Symfony2 404 handling, you will have to set the following parameter to true:

hn_entities_enable_symfony2_404: true