Dependency injection : Get Param in controller

398 views Asked by At

I am in the process of migrating an application from Symfony 2.8 to Symfony 3.4

The services are now private and therefore instead of making a direct call to the services from the container, we must use dependency injection as a workaround.

For parameters, is $this->getParameter() is an anti pattern ? If yes, how can we get them in controller and of course we must always respecting the good practices ?

For information, there is a solution in Symfony 4.1, using the bind in services.yml

This the following script and this the environment as an example :

class CmsController extends Controller
{
    /**
     * @param Request $request
     *
     * @return Response
     */
    public function importExportAction(Request $request): Response
    {
        $adminPool = $this->get('sonata.admin.pool');
        $env = $this->getParameter('environment');

         return new Response('OK');
    }
}
1

There are 1 answers

1
GrenierJ On BEST ANSWER

Controllers are now seen as services.

You can look at this anwser, to know how configure services with parameters.