Pergunta

I'm pretty new to zf2 but I already setup a site working with it. I got some understanding of the serviceManager but now I'm stuck.

Here is the context : I want to implement a logger available on any class of my zf2 application.

In my global.php I create the factory for the logger :

'service_manager' => array(
    'factories' => array(
        'Zend\Db\Adapter\Adapter'
                => 'Zend\Db\Adapter\AdapterServiceFactory',
        'Zend\Log\Logger' => function($sm){
            $logger = new Zend\Log\Logger;
            $writer = new Zend\Log\Writer\Stream('./data/log/'.date('Y-m-d').'-error.log','wb'); 
            $logger->addWriter($writer);     
            return $logger;
        },
    ),

Now I want to have it injected in every class implementing LoggerAwareInterface. So in my Module.php I have this initializer in my getServiceConfig function

        'initializers' => array(
                'logger' => function($service, $sm) {
                    if ($class instanceof LoggerAwareInterface) {
                        $logger = $sm->get('Zend\Log\Logger');
                        $class->setLogger($logger);
                    }
                }
        ),

Example given, I want to inject it in a class named PartController, so I set it as an invokable in module.config.php

return array(
    'controllers' => array(
        'invokables' => array(
            'Part\Controller\Part' => 'Part\Controller\PartController',
        ),
    ),

This class is implementing LoggerAwareInterface

class PartController extends AbstractActionController implements LoggerAwareInterface

The issue I have is that the logger is not initialized in PartController, what I checked with a var_dump in my PartController.

I tried to dump all the services checked by the initializer, and PartController doesn't appear ...

What am I doing wrong ? And why is the PartController not registered in the serviceManager although it is in the invokables section of my module.config.php ?

Thanks in advance to everyone.

Foi útil?

Solução

If you want your initializer to apply to a controller, you need to tell the ControllerManager about it, you can do that by implementing the getControllerConfig method defined by Zend\ModuleManager\Feature\ControllerProviderInterface, ie.,

<?php
// ..some namespace

use Zend\ModuleManager\Feature\ControllerProviderInterface;

class Module implements ControllerProviderInterface
{
    // .. 

    public function getControllerConfig()
    {
        return array(
            'initializers' => array(
                'LoggerAwareInitializer' => function($instance, $sm) {
                    if ($instance instanceof LoggerAwareInterface) {
                        $logger = $sm->getServiceLocator()->get('Zend\Log\Logger');
                        $instance->setLogger($logger);
                    }
                },
            ),
        );
    }

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top