我似乎无法让我的表网关注入我的存储库(服务)...
我有以下几点:
模块.php:
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Album'Model'Concrete'AlbumRepository' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumRepository($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend'Db'Adapter'Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('albums', $dbAdapter, null, $resultSetPrototype);
}
)
);
}
这是我的模块.php:
return array(
'service_manager' => array(
'invokables' => array(
'Album'Model'Abstracts'IAlbumRepository' => 'Album'Model'Concrete'AlbumRepository'
),
),
'controllers' => array(
'factories' => array(
'Album'Controller'Album' => 'Album'Model'Factories'AlbumControllerFactory',
),
),
错误是:
Catchable fatal error: Argument 1 passed to Album'Model'Concrete'AlbumRepository::__construct() must be an instance of Zend'Db'TableGateway'TableGateway, none given, called in C:'xampp'htdocs'ZendFrameworkTest'vendor'zendframework'zendframework'library'Zend'ServiceManager'ServiceManager.php on line 1035 and defined in C:'xampp'htdocs'ZendFrameworkTest'module'Album'src'Album'Model'Concrete'AlbumRepository.php on line 11
注意:
工厂数组上的委托函数只是没有被调用,我在做一些愚蠢的事情,但我无法说出是什么。
我也在做依赖注入,我猜这是出错的地方,因为工厂在没有注入的情况下创建存储库对象:
class AlbumControllerFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
*
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
// Need to do something here?
$realServiceLocator = $serviceLocator->getServiceLocator();
$postService = $realServiceLocator->get('Album'Model'Abstracts'IAlbumRepository');
return new AlbumController($postService);
}
}
您将存储库定义为可调用,这意味着服务管理器尝试通过直接实例化来创建它,而无需任何参数。
将其更改为别名
return array(
'service_manager' => array(
'aliases' => array(
'Album'Model'Abstracts'IAlbumRepository' => 'Album'Model'Concrete'AlbumRepository'
),
),
);