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
- Improve exception message when AbstractController::getParameter fails Fixes #27436 — committed to curry684/symfony by curry684 6 years ago
- minor #27443 [DX] Improve exception message when AbstractController::getParameter fails (curry684) This PR was squashed before being merged into the 4.1 branch (closes #27443). Discussion ----------... — committed to symfony/symfony by deleted user 6 years ago
- feature #27462 [FrameworkBundle] Deprecate auto-injection of the container in AbstractController instances (nicolas-grekas) This PR was merged into the 4.2-dev branch. Discussion ---------- [Framew... — committed to symfony/symfony by nicolas-grekas 6 years ago
That error has no lack of DX at all and explicitly tells you how to fix it.