Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a baseclass for Symfony2 controllers

There are some articles around which touch this topic, but none of them seemed to be a practical usable solution for me. My goal is to put some basic methods (those I need in each controller anyway) into a basecontroller, e.g.

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

How can this be done?

AFAIK we have to inject the services into the basecontroller, but how do I tell classes to use a service for their superclass? There is some decent article about Controllers and Dependency Injection [1], but finally I got stuck with that 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

like image 320
stoefln Avatar asked Apr 27 '11 08:04

stoefln


1 Answers

pseudocode

MyBaseController impliments Symfony\Component\Di\ContainerAwareInterface
  setContainer($container)
    $this->container = $container

  getEntityManager
    return $this->container->get('doctrine.orm.entity_manager')
like image 152
Koc Avatar answered Nov 15 '22 07:11

Koc