-1

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');
    }
}
hbgamra
  • 749
  • 1
  • 6
  • 18
  • 1
    Hello, you should consider your controllers as services. Then you only have to configure them in yaml files like explained [here](https://stackoverflow.com/a/11211620/6410457) – GrenierJ Oct 13 '20 at 14:48

1 Answers1

0

Controllers are now seen as services.

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

GrenierJ
  • 1,145
  • 8
  • 18
  • 1
    Down voted because link only answers are discouraged. It's also interesting that the answer linked dates back to 2016. So the "now seen" part is a bit strange. Controllers could always be configured as services though using the container as a service locator was the original recommended approach. – Cerad Oct 13 '20 at 18:04