【问题标题】:ZF3 Introducing Blog Module are you certain you provided it during configurationZF3 Introducing Blog Module 你确定你在配置时提供了它吗
【发布时间】:2017-09-18 04:17:24
【问题描述】:

所以我正在尝试学习 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/

教程在“Writing a Factory Class”这一点上说,这个错误消息是预期的,但在“Registering Services”它应该得到修复,好吧,它没有。

我的模块.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;
   }
}

【问题讨论】:

    标签: php zend-framework zend-framework3


    【解决方案1】:

    根据您在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));
        }
    }
    

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

    【讨论】:

    • 嗨,多莉,首先感谢您的回答,并为我迟到的回答感到抱歉。我实际上已经创建了一个 ListControllerFactory 但返回不同:public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { return new ListController($container-&gt;get(PostRepositoryInterface::class)); }
    • 没问题,我认为回报是一样的。因为您使用alias 名称访问。那么,您还有问题吗?
    • 是的,还是同样的问题。
    • @Kden,嗯,如果您创建了 Factory 并且仍然得到相同的错误 Unable to resolve service "Blog\Controller\ListController" to a factory; are you certain you provided it during configuration? 我只是认为您需要更新缓存配置。直接删除即可,因为文件不存在时会重新创建rm data/cache/module-config-cache.application.config.cache.php
    • 你能解决这个@Kden吗?如果是,解决方案是什么?
    【解决方案2】:

    请删除该行:

    use Zend\Config\Factory;
    

    Factory 命名空间与此类冲突。

    【讨论】:

      【解决方案3】:

      检查 \config\autoload\mezzio-tooling-factories.global.php 文件,您的 Factory 类条目可能丢失。 这是我最后的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-27
        • 1970-01-01
        • 2010-10-20
        • 1970-01-01
        • 2019-11-03
        • 1970-01-01
        • 2019-04-05
        相关资源
        最近更新 更多