aufziehvogel
aufziehvogel

Reputation: 7297

Controller does not get called in initializers closure

I want to put the doctrine entity manager into many different classes in my ZF2 project. Thus. I have setup the following initializer in my Module.php:

'initializers' => array(
      function ($instance, $services) {
            if (is_object($instance)) { // just for debugging
                var_dump(get_class($instance));
            }
            if (!$instance instanceof EntityManagerAwareInterface) {
                return;
            }

            $entityManager = $services->get('doctrine.entitymanager.orm_default');
            $instance->setEntityManager($entityManager);
        },
    ),
)

Yet, it never gets called on my AuthController even though I am visiting a site of that controller (and get a null pointer exception, because the entity-manager was not set). Of course, the controller does implement the required interface:

class AuthController extends AbstractActionController implements EntityManagerAwareInterface

Is there anything else I have to configure so that my AuthController is checked against the initializer closure?

At the moment I have it under invokables in module.config.php.

'controllers' => array(
    'invokables' => array(
        'Auth\Controller\Auth' => 'Auth\Controller\AuthController',
    ),
),

When I remove it from there, the application cannot find the class anymore.

My debugging output lists other classes which are checked against the initializers, many managers and services. A small excerpt:

string(37) "Zend\\Mvc\\Controller\\ControllerManager"
string(33) "Zend\\Mvc\\Controller\\PluginManager"
string(29) "Zend\\View\\HelperPluginManager"
[...]
string(24) "Doctrine\\DBAL\\Connection"
string(26) "Doctrine\\ORM\\EntityManager"
string(41) "Zend\\Authentication\\AuthenticationService"

Upvotes: 3

Views: 673

Answers (1)

Crisp
Crisp

Reputation: 11447

Try adding an initializer for the controller manager too, judging by your debug output, the one you posted appears to be for the service manager. You configure other managers the same way, the method to use for controller manager is getControllerConfig

 public function getControllerConfig()
 {
      return array(
          'initializers' => array(
              // controller initializers here...
          ),
      );
 }

Upvotes: 3

Related Questions