How to implement a base class for Symfony2 controllers

There are several articles that relate to this topic, but none of them represented a practical solution for me. My goal is to put some basic methods (the ones that I need in each controller anyway) in the base controller, for example.

$this->getEntityManager(); $this->getRequest(); $this->getRepository($entityName); 

How can I do that?

AFAIK, we need to implement services in the base controller, but how can I say that classes use the service for their superclass? There is some decent article on controllers and dependency injection [1], but finally I am stuck with this approach too, see my comment here: [2]

[1] http://miller.limethinking.co.uk/2011/04/15/symfony2-controller-as-service/

[2] http://miller.limethinking.co.uk/2011/04/15/symfony2-controller-as-service/#comment-579

+6
source share
1 answer

pseudo code

 MyBaseController impliments Symfony\Component\Di\ContainerAwareInterface setContainer($container) $this->container = $container getEntityManager return $this->container->get('doctrine.orm.entity_manager') 
+3
source

Source: https://habr.com/ru/post/886775/


All Articles