zend framework2 - ZendFramework 2 - Problems with AbstractFactoryInterface; Param doesn't contain route/controller Namespace -
i've got module 1 route , abstract controller factory.
the route
namespace baseapi; use baseapi\factory\controller\abstractapicontrollerfactory; use zend\mvc\router\http\segment; return [ 'router' => [ 'routes' => [ 'v2a-api' => [ 'type' => segment::class, 'options' => [ 'route' => '/api/:controller', 'defaults' => [ '__namespace__' => 'baseapi\controller', ], ], ], ], ], 'controllers' => [ 'abstract_factories' => [ abstractapicontrollerfactory::class, ], ], ];
the abstract controller factory
namespace baseapi\factory\controller; use baseapi\controller\apicontroller; use baseapi\exception\apiabstractcontrollerfactoryexception; use zend\filter\inflector; use zend\filter\stringtolower; use zend\filter\word\camelcasetodash; use zend\servicemanager\abstractfactoryinterface; use zend\servicemanager\servicelocatorinterface; /** * class abstractapicontrollerfactory * * @package baseapi\factory\controller */ class abstractapicontrollerfactory implements abstractfactoryinterface { /** * @param \zend\servicemanager\servicelocatorinterface $controllermanager * @param $name * @param $requestedname * * @return bool */ public function cancreateservicewithname(servicelocatorinterface $controllermanager, $name, $requestedname) { dump('api', $name, $requestedname); $servicemanager = $controllermanager->getservicelocator(); $configkey = $this->getconfigkey($requestedname); $config = $servicemanager->get('config'); return ( strpos($requestedname, 'baseapi\controller') === 0 && key_exists( $configkey, !empty($config['d3-api']) ? $config['d3-api'] : [] ) ); } /** * @param \zend\servicemanager\servicelocatorinterface $controllermanager * @param $name * @param $requestedname * * @return \baseapi\controller\apicontroller * @throws \baseapi\exception\apiabstractcontrollerfactoryexception */ public function createservicewithname(servicelocatorinterface $controllermanager, $name, $requestedname) { $servicemanager = $controllermanager->getservicelocator(); $configkey = $this->getconfigkey($requestedname); $config = $servicemanager->get('config'); if (empty($config['d3-api'][$configkey]['service'])) { throw apiabstractcontrollerfactoryexception::missingservicekey($configkey); } if (!$servicemanager->has($config['d3-api'][$configkey]['service'])) { throw apiabstractcontrollerfactoryexception::missingserviceexception( $config['d3-api'][$configkey]['service'] ); } return new apicontroller( $servicemanager->get($config['d3-api'][$configkey]['service']) ); } /** * transforms controller-name + namespace $requestedname in related config-key * :: baseapi\controller\gamelists => game-lists * * @param $requestedname * * @return string */ protected function getconfigkey($requestedname) { $controllername = substr($requestedname, strrpos($requestedname, '\\') + 1); $inflector = new inflector(':classname'); $inflector->setfilterrule( ':classname', [ camelcasetodash::class, stringtolower::class, ] ); return $inflector->filter(['classname' => $controllername]); } }
additional config (doesn't matter problem, completeness sake)
<?php return [ 'd3-api' => [ 'customers' => [ 'service' => 'v2acustomerdb\service\customers', ], ], ];
now problem: i've got 2 zf-mvc-apps. in one, works fine; see objects when start request under /api/customers
. in other one, parameter $requestedname of abstractapicontrollerfactory::cancreateservicewithname doesn't contain namespace. receive customers
instead of baseapi\controller\customers
the project-setup same. installed on composer has same version. has config-bug, after several hours, i've got no clue, might be...
someone suggestions?
after 2 days...
module.php has (add bootstraplistenerinterface)
<?php /** * zend framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zendskeletonapplication canonical source repository * @copyright copyright (c) 2005-2014 zend technologies usa inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd new bsd license */ namespace customerdatabase; use zend\eventmanager\eventinterface; use zend\modulemanager\feature\bootstraplistenerinterface; use zend\mvc\moduleroutelistener; /** * class module * * @package customerdatabase */ class module implements bootstraplistenerinterface { public function onbootstrap(eventinterface $event) { $eventmanager = $event->getapplication()->geteventmanager(); $moduleroutelistener = new moduleroutelistener(); $moduleroutelistener->attach($eventmanager); } [...] }
Comments
Post a Comment