"Unable to resolve service "Album\Controller\AlbumController" 到工厂;你确定你在配置时提供了它吗?"在 Zend Framework 2 中

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

我在 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 的新手。只是无法弄清楚我做错了什么。请提及是否需要更多代码。

您已经为 "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)
                );
            },
        ],
    ];
}

他们都是互相矛盾的。可调用工厂用于 类 没有任何依赖项。在这种情况下,看起来 "AlbumController" 具有 "AlbumTable" 的依赖性,因此第二个选项看起来就像您想要的那样。

简而言之,从 module.config.php 中的数组中删除 'controllers' 键值。

希望对您有所帮助!

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

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