zend framework2 - Class not recognized by initializer -
i'm pretty new zf2 setup site working it. got understanding of servicemanager i'm stuck.
here context : want implement logger available on class of zf2 application.
in global.php create factory 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 want have injected in every class implementing loggerawareinterface. in module.php have initializer in getserviceconfig function
'initializers' => array( 'logger' => function($service, $sm) { if ($class instanceof loggerawareinterface) { $logger = $sm->get('zend\log\logger'); $class->setlogger($logger); } } ),
example given, want inject in class named partcontroller, set invokable in module.config.php
return array( 'controllers' => array( 'invokables' => array( 'part\controller\part' => 'part\controller\partcontroller', ), ),
this class implementing loggerawareinterface
class partcontroller extends abstractactioncontroller implements loggerawareinterface
the issue have logger not initialized in partcontroller, checked var_dump in partcontroller.
i tried dump services checked initializer, , partcontroller doesn't appear ...
what doing wrong ? , why partcontroller not registered in servicemanager although in invokables section of module.config.php ?
thanks in advance everyone.
if want initializer apply controller, need tell controllermanager
it, can implementing getcontrollerconfig
method defined 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); } }, ), ); } }
Comments
Post a Comment