首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无效的工厂注册ZendFramework2教程,一切似乎已经就绪

无效的工厂注册ZendFramework2教程,一切似乎已经就绪
EN

Stack Overflow用户
提问于 2015-10-22 15:12:51
回答 1查看 428关注 0票数 1

这是我的module/Blog/config/module.config.php

代码语言:javascript
复制
<?php
    return array(
        'service_manager' => array(
            'invokables' => array(
                'Blog\Service\PostServiceInterface' => 'Blog\Service\PostService'
            )
        ),
        'controllers' => array(
            'factories' => array(
                'Blog\Controller\List' => 'Blog\Controller\ListControllerFactory'
            )
        ),
    'router' => array(
        // Open configuration for all possible routes
        'routes' => array(
            // Define a new route called "post"
            'post' => array(
                // Define the routes type to be "Zend\Mvc\Router\Http\Literal", which is basically just a string
                'type' => 'literal',
                // Configure the route itself
                'options' => array(
                    // Listen to "/blog" as uri
                    'route'    => '/blog',
                    // Define default controller and action to be called when this route is matched
                    'defaults' => array(
                        'controller' => 'Blog\Controller\List',
                        'action'     => 'index',
                    )
                )
            )
        )
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    )
);

这是我的module/Blog/src/Blog/Controller/ListController.php

代码语言:javascript
复制
    <?php
    namespace Blog\Controller;

    use Blog\Service\PostServiceInterface;
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;

    class ListController extends AbstractActionController
    {
        protected $postService;

        public function __construct(PostServiceInterface $postService)
        {
            $this->postService = $postService;
        }

        public function indexAction()
        {
            return new ViewModel(array(
                'posts' => $this->postService->findAllPosts()
            ));
        }
    }

这是我的module/Blog/src/Blog/Factory/ListControllerFactory.php

代码语言:javascript
复制
    <?php
    namespace Blog\Factory;

    use Blog\Controller\ListController;
    use Zend\ServiceManager\FactoryInterface;
    use Zend\ServiceManager\ServiceLocatorInterface;

    class ListControllerFactory implements FactoryInterface
    {
        public function createService(ServiceLocatorInterface $serviceLocator)
        {
            $realServiceLocator = $serviceLocator->getServiceLocator();
            $postService = $realServiceLocator->get('Blog\Service\PostServiceInterface');
            return new ListController($postService);
        }
    }

最后,这是我的module/Blog/Module.php

代码语言:javascript
复制
    <?php
    namespace Blog;

    use Zend\ModuleManager\Feature\ConfigProviderInterface;
    use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
    use CodeGenerationUtils\Autoloader\AutoloaderInterface;

    class Module implements ConfigProviderInterface,AutoloaderProviderInterface
    {
        public function getConfig()
        {
            return include __DIR__ . '/config/module.config.php';
        }

        public function getAutoloaderConfig()
        {
            return array(
                'Zend\Loader\StandardAutoloader' => array(
                    'namespaces' => array(
                        // Autoload all classes from namespace 'Blog' from '/module/Blog/src/Blog'
                        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                    )
                )
            );
        }
    }

我得到了错误:

代码语言:javascript
复制
While attempting to create blogcontrollerlist(alias: Blog\Controller\List) an invalid factory was registered for this instance type.

不知道这里出了什么问题,一切似乎都就绪了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-22 15:17:44

在您的配置中,您指向Blog\Controller\ListControllerFactory,而您的文件位于Blog\Factory\ListControllerFactory中。

像这样更新,它会工作的:

代码语言:javascript
复制
'controllers' => array(
    'factories' => array(
        'Blog\Controller\List' => 'Blog\Factory\ListControllerFactory'
    )
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33284482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档