php - SF 4.4 - XXXController::getParameter() method is missing a parameter bag to work properly

Issue :

I recently encountered a blocking issue in my Symfony Project. I just upgraded from 3.4 to 4.4. I followed the guidelines and replaced all deprecated notices.

I have a huge and dirty controller for a side feature.

As written in SF doc I replaced :

class XXXController extends Controller

by

class XXXController extends AbstractController

In my logic the function getParameter() from src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php is called.

   protected function getParameter(string $name)
{
    if (!$this->container->has('parameter_bag')) {
        throw new ServiceNotFoundException('parameter_bag.', null, null, [], sprintf('The "%s::getParameter()" method is missing a parameter bag to work properly. Did you forget to register your controller as a service subscriber? This can be fixed either by using autoconfiguration or by manually wiring a "parameter_bag" in the service locator passed to the controller.', static::class));
    }

    return $this->container->get('parameter_bag')->get($name);
}

But I always fall in the 'if' condition.

I tried :

Auto-wiring declaration in my service.yml like :

services:
_defaults:
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

To overwrite the getParameters() in my controller.

To declare my controller has a service but it began to get really messy because the targeted method is more than 1k lines long.

I tried to bind it as a common argument in service.yml

Actual Work Around

Now my only solution is to keep extending the old Symfony\Bundle\FrameworkBundle\Controller for every controller where I need to call getParameter() on the container.

Is there an easy way for injecting Symfony\Component\DependencyInjection\ParameterBag\ParameterBag in my targeted controller ? Am I missing something obvious ?

EDIT :

  • When I try

php bin/console debug:container parameter_bag

I got :

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

There is an issue with the notice about the service been removed from container ? I can't find why tho.

  • When i try :

{-code-6}

I got :

{-code-7}

I declared it in my service.yml

{-code-8}

__construct() method in XXXController

 {-code-9}

But using this solution I always get the error :

 {-code-10}

Am I stuck in a loop ? Defining in the constructor method and service.yml is not enought ? How can I provide the ParameterBag ?

NB :

  • It's an edit method with an {id} as route param
  • I already cleanned cache multiple times & did a composer_update & checked for issues between dependencies on packagist.

Answer

Answer

Answer

Answer

Answer

Answer

Answer

Answer

- Option Value

Answer

Answer

Answer

Answer

Answer

Answer

Answer

Answer

- 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

Answer

Answer

Answer

Answer

Answer

Answer

Answer

Answer

- ! [NOTE] The "parameter_bag" service or alias has been removed or inlined when the container was compiled.|||bin/console debug:container XXXController|||Information for Service "xxx_controller" =========================================

Answer

Answer

Answer

Answer

Answer

Answer

-- Option Value

Answer

Answer

Answer

Answer

Answer

Answer

-- Service ID xxx_controller Class AppBundle\Controller\Backend\XXXController Tags controller.service_arguments container.service_subscriber Calls setContainer Public yes Synthetic no Lazy no Shared yes Abstract no Autowired yes Autoconfigured yes|||xxx_controller: class: AppBundle\Controller\Backend\XXXController arguments: - $parameterBag : '@parameter_bag' public: true|||/** * @var ParameterBag */ private $parameterBag; public function __construct(ParameterBag $parameterBag) { $this->parameterBag = $parameterBag; }|||XXXController has required constructor arguments and does not exist in the container. Did you forget to define the controller as a service?

Source