symfony: [4.1] The "parameter_bag" service is not available.

Symfony version(s) affected: 4.1.0

Description
Since updated to Symfony 4.1, I switch to use AbstractController instead of Controller class, as suggested!

However, I receive now the LogicExtension: The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1".

It seems the the service parameter_bag is not public and cannot be acceded!

Using the debug command: php bin/console debug:container parameter_bag

Information for Service "parameter_bag"
=======================================

 ---------------- ----------------------------------------------------------------- 
  Option           Value                                                            
 ---------------- ----------------------------------------------------------------- 
  Service ID       parameter_bag                                                    
  Class            Symfony\Component\DependencyInjection\ParameterBag\ContainerBag  
  Tags             -                                                                
  Public           no                                                               
  Synthetic        no                                                               
  Lazy             no                                                               
  Shared           yes                                                              
  Abstract         no                                                               
  Autowired        no                                                               
  Autoconfigured   no                                                               
 ---------------- ----------------------------------------------------------------- 

How to reproduce

class TestController extends AbstractController
{
    public function index()
    {
       // ...
       $variable = $this->getParameter('service_name');
       // ...
    }
}

Possible Solution
In the file: framework-bundle/Resources/config/services.xml

Change:

<service id="parameter_bag" class="Symfony\Component\DependencyInjection\ParameterBag\ContainerBag">
        <argument type="service" id="service_container" />
</service>

By:

<service id="parameter_bag" class="Symfony\Component\DependencyInjection\ParameterBag\ContainerBag" public="true">
        <argument type="service" id="service_container" />
 </service>

To make the service has public seems to resolve the issue!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 20 (14 by maintainers)

Commits related to this issue

Most upvoted comments

That error has no lack of DX at all and explicitly tells you how to fix it.