Thursday, 15 January 2015

php - ZF2 - Loading Models in Service Manager -



php - ZF2 - Loading Models in Service Manager -

in module config file, have these lines loads libraries , models servicemanager can retrieve them in controllers. can see models require same dependency. may ask, how can inject them without these repetitive blocks of code? looks wrong me not know how run same lines of code different libraries. or should using factories @ all? give thanks you!

'service_manager' => array( 'abstract_factories' => array( 'zend\cache\service\storagecacheabstractservicefactory', 'zend\log\loggerabstractservicefactory', ), 'aliases' => array( 'translator' => 'mvctranslator', ), 'factories' => array( // models 'application\model\photo' => function($servicelocator) { $corelibrary = $servicelocator->get('project\corelibrary'); $io = $servicelocator->get('project\io'); $class = new application\model\photo($corelibrary, $io); homecoming $class; }, 'application\model\album' => function($servicelocator) { $corelibrary = $servicelocator->get('project\corelibrary'); $io = $servicelocator->get('project\io'); $class = new application\model\album($corelibrary, $io); homecoming $class; }, 'application\model\tag' => function($servicelocator) { $corelibrary = $servicelocator->get('project\corelibrary'); $io = $servicelocator->get('project\io'); $class = new application\model\tag($corelibrary, $io); homecoming $class; }, 'application\model\user' => function($servicelocator) { $corelibrary = $servicelocator->get('project\corelibrary'); $io = $servicelocator->get('project\io'); $class = new application\model\user($corelibrary, $io); homecoming $class; }, 'application\model\message' => function($servicelocator) { $corelibrary = $servicelocator->get('project\corelibrary'); $io = $servicelocator->get('project\io'); $class = new application\model\message($corelibrary, $io); homecoming $class; }, ), ),

you should consider utilize abstractfactory in case. see code below:

namespace application\model; utilize zend\servicemanager\abstractfactoryinterface; utilize zend\servicemanager\servicelocatorinterface; class abstractmodelfactory implements abstractfactoryinterface { public function cancreateservicewithname(servicelocatorinterface $servicelocator, $name, $requestedname) { $requestedname = explode('\\', $requestedname); if ((!isset($requestedname[0]) || $requestedname[0] != 'application') || (!isset($requestedname[1]) || $requestedname[1] != 'model')) { homecoming false; } $modelname = $requestedname[2]; homecoming class_exists('application\model\\'.$modelname); } public function createservicewithname(servicelocatorinterface $servicelocator, $name, $requestedname) { $requestedname = explode('\\', $requestedname); $modelname = $requestedname[2]; $corelibrary = $servicelocator->get('project\corelibrary'); $io = $servicelocator->get('project\io'); $class = 'application\model\\'.$modelname; homecoming new $class($corelibrary, $io); } }

then register mill so:

'service_manager' => [ 'abstract_factories' => [ 'application\model\abstractmodelfactory' ] ]

php zend-framework2 servicemanager

No comments:

Post a Comment