Symfony2: InactiveScopeException and request

Error message: You cannot create a service (“request”) of an inactive scope (“request”).

You cannot create a service ("request") of an inactive scope ("request").

This message is written, when you’re invoking a command on the command line, e.g.

php app/console assetic:dump

Reason: you are using a request object, but this is not available on the command line / cli

The error was in my case that I used an object in my service configuration that references the request object (MobileDetectBundle).

Solution: remove reference for command line

Step 1 was to use a setter for the reference

Step 2 was to create a special compiler pass in the bundle configuration

Source code (based on – thanks suncat2000!)


namespace Acm\DemoBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

 * CLI listener
class CLIListenerPass implements CompilerPassInterface
     * If CLI, when remove setMobileDetector method from definition mobile_detect.twig.extension
     * @param ContainerBuilder $container 
    public function process(ContainerBuilder $container)
        if (php_sapi_name() == "cli") {
            $definition = $container->getDefinition('acme_demo.extension.my_service');