Symfony Bundle: A ContainerAware compiler pass to automatically inject your service container.

1.0.3 2018-01-24 09:25 UTC

This package is auto-updated.

Last update: 2020-06-14 15:27:07 UTC


Latest Stable Version License Build Status Quality Score Total Downloads

This Symfony bundle automatically injects the Service Container into all your services that implement Symfony\Component\DependencyInjection\ContainerAwareInterface.


composer require bentools/containeraware-bundle

Then, just add this bundle to your AppKernel.

# app/AppKernel.php

class AppKernel extends Kernel
    public function registerBundles()
        $bundles = [
            // ...
            new BenTools\ContainerAwareBundle\ContainerAwareBundle(), // Insert at the bottom of the array for better performances

        return $bundles;

And that's it! You're ready to go. No need to edit any configuration file.

You no longer need to explicitely call $service->setContainer($container) in your services.yml or services.xml files.

Example usage

# src/AppBundle/Services/DummyService.php

namespace AppBundle\Services;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

class DummyService implements ContainerAwareInterface
    use ContainerAwareTrait;

    public function doSomethingAwesome()
        $doctrine = $this->container->get('doctrine');
        // do awesome stuff
# app/config/services.yml

        class: AppBundle\Services\DummyService
            #- [ 'setContainer', [ '@service_container' ] ] # // Not needed anymore


This bundle has been successfully tested against Symfony 2.7 to 3.3 / PHP 5.3 to 7.1.

See Travis builds for more information.