dongxun6690 2017-04-10 05:31
浏览 48
已采纳

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

I am getting an error in Zend Framework 2 tutorial project as,

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

My Module.php as

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

And my module.config.php is;

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

I am very new in Zend Framework. Just can not figure out what I am doing wrong. Please mention if some more code is necessary.

  • 写回答

2条回答 默认 最新

  • douhua9726 2017-04-10 11:01
    关注

    You have defined two factories for "Controller\AlbumController::class"...

    One in module.config.php -

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

    and the second one in Module.php-

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

    They are both conflicting with each other. Invokable factories are for classes that doesn't have have any dependencies. In this case it looks like "AlbumController" has a dependency of "AlbumTable", so the second option looks like the one you want.

    So in short, remove the 'controllers' key value from the array in module.config.php.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP