ZF3介绍博客模块您确定在配置期间提供了它

时间:2017-04-21 08:46:03

标签: php zend-framework zend-framework3

所以即时通讯尝试学习zend框架3,我现在因为几个小时而错过了这个错误:

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

我正在学习的教程: https://docs.zendframework.com/tutorials/in-depth-guide/models-and-servicemanager/

教程在“编写工厂类”这一点上说这个错误信息是预期的,但是在“注册服务”它应该得到修复,好吧,它没有。

我的module.config.php:

<?php 
namespace Blog;

use Zend\Config\Factory;
use Zend\Router\Http\Literal;
use Zend\ServiceManager\Factory\InvokableFactory;


return [

'service_manager' => [
    'aliases' => [
        Model\PostRepositoryInterface::class => Model\PostRepository::class,
    ],
    'factories' => [
        Model\PostRepository::class => InvokableFactory::class,
    ],
],
'controllers' => [
    'factories' => [
        // Update the following line:
        Controller\ListController::class => 
        Factory\ListControllerFactory::class,
    ],
],
'router' => [
    'routes' => [
        'blog' => [
            'type' => Literal::class,
            'options' => [
                'route' => '/blog',
                'defaults' => [
                    'controller' => Controller\ListController::class,
                    'action' => 'index',
                ],
            ],
        ],
    ],
],
'view_manager' => [
    'template_path_stack' => [
        __DIR__ . '/../view',
    ],
],

];

我的ListController.php:

namespace Blog\Controller;

use Blog\Model\PostRepositoryInterface;
use Zend\Mvc\Controller\AbstractActionController;

class ListController extends AbstractActionController
{
   /**
   * @var PostRepositoryInterface
   */
   private $postRepository;

   public function __construct(PostRepositoryInterface $postRepository)
   {
      $this->postRepository = $postRepository;
   }
}

2 个答案:

答案 0 :(得分:0)

根据module.config.php中的代码,类Factory\ListControllerFactory应该存在。

'controllers' => [
    'factories' => [
        // Update the following line:
        Controller\ListController::class => Factory\ListControllerFactory::class,
    ],
],

从错误消息中,它表示未创建Blog\Controller\ListController控制器。我想因为没有工厂级别。

您可以像这样创建Blog\Controller\ListControllerFactory类,

<?php
namespace Blog\Controller;

use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;

class ListControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        return new ListController($container->get(Blog\Model\PostRepository::class));
    }
}

请尝试,我认为这会有所帮助

答案 1 :(得分:0)

请删除该行:

use Zend\Config\Factory;

Factory 名称空间与此类冲突。