KOU*_*DAL 0 php zend-framework
我在 Zend Framework 2 教程项目中遇到错误,因为,
Unable to resolve service "Album\Controller\AlbumController" to a factory; are you certain you provided it during configuration?
Run Code Online (Sandbox Code Playgroud)
我的 Module.php 作为
namespace Album;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig()
{
return [
'factories' => [
Model\AlbumTable::class => function($container) {
$tableGateway = $container->get(Model\AlbumTableGateway::class);
return new Model\AlbumTable($tableGateway);
},
Model\AlbumTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
],
];
}
public function getControllerConfig()
{
return [
'factories' => [
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController(
$container->get(Model\AlbumTable::class)
);
},
],
];
}
}
Run Code Online (Sandbox Code Playgroud)
而我的 module.config.php 是;
namespace Album;
use Zend\Router\Http\Segment;
//use Zend\ServiceManager\Factory\InvokableFactory;
return [
'controllers' => [
'factories' => [
Controller\AlbumController::class => InvokableFactory::class,
],
],
'router' => [
'routes' => [
'album' => [
'type' => Segment::class,
'options' => [
'route' => '/album[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\AlbumController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
'album' => __DIR__ . '/../view',
],
],
];
Run Code Online (Sandbox Code Playgroud)
我对 Zend 框架很陌生。只是无法弄清楚我做错了什么。请提及是否需要更多代码。
您已经为“Controller\AlbumController::class”定义了两个工厂...
一个在 module.config.php -
return [
'controllers' => [
'factories' => [
Controller\AlbumController::class => InvokableFactory::class,
],
],
],
Run Code Online (Sandbox Code Playgroud)
和第二个在 Module.php-
public function getControllerConfig()
{
return [
'factories' => [
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController(
$container->get(Model\AlbumTable::class)
);
},
],
];
}
Run Code Online (Sandbox Code Playgroud)
它们彼此冲突。可调用工厂用于没有任何依赖关系的类。在这种情况下,它看起来像“AlbumController”依赖于“AlbumTable”,所以第二个选项看起来像你想要的。
简而言之,从module.config.php 中的数组中删除'controllers' 键值。
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
9182 次 |
| 最近记录: |