"无法解析服务" Album \ Controller \ AlbumController"到工厂;你确定你在配置期间提供了吗?"在Zend Framework 2中

时间:2017-04-10 05:31:21

标签: php zend-framework

我在Zend Framework 2教程项目中收到错误,

Unable to resolve service "Album\Controller\AlbumController" to a factory; are you certain you provided it during configuration?

我的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)
                );
            },
        ],
    ];
}
}

我的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',
    ],
],
];

我是Zend Framework的新手。只是无法弄清楚我做错了什么。如果需要更多代码,请提及。

2 个答案:

答案 0 :(得分:3)

您已为" Controller \ AlbumController :: class" ...

定义了两个工厂

在module.config.php中有一个 -

return [
    'controllers' => [
        'factories' => [
             Controller\AlbumController::class => InvokableFactory::class,
        ],
     ],
],

和Module.php中的第二个 -

public function getControllerConfig()
{
    return [
        'factories' => [
            Controller\AlbumController::class => function($container) {
                return new Controller\AlbumController(
                    $container->get(Model\AlbumTable::class)
                );
            },
        ],
    ];
}

他们彼此冲突。 Invokable工厂适用于没有任何依赖关系的类。在这种情况下,它看起来像" AlbumController"具有" AlbumTable"的依赖关系,因此第二个选项看起来像你想要的那个。

简而言之,请删除“控制器”' module.config.php中数组的键值。

希望这有帮助!

答案 1 :(得分:2)

我遇到了同样的问题,以及其他一些相关的问题 - 直到我终于意识到问题的真正根源:我正在关注Zend 2的教程,而我安装的是Zend 3!

如果你有Zend 3,请务必使用Zend 3的教程,这里: https://docs.zendframework.com/tutorials/getting-started/overview/