Php 工厂注册的Zend Framework 2教程无效,所有内容似乎都已就绪

Php 工厂注册的Zend Framework 2教程无效,所有内容似乎都已就绪,php,zend-framework2,factory,Php,Zend Framework2,Factory,这是我的module/Blog/config/module.config.php <?php return array( 'service_manager' => array( 'invokables' => array( 'Blog\Service\PostServiceInterface' => 'Blog\Service\PostService' )

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

<?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',
        ),
    )
);
    <?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()
            ));
        }
    }
    <?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);
        }
    }
    <?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__,
                    )
                )
            );
        }
    }
这是我的
模块/Blog/src/Blog/Factory/ListControllerFactory.php

<?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',
        ),
    )
);
    <?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()
            ));
        }
    }
    <?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);
        }
    }
    <?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__,
                    )
                )
            );
        }
    }
我收到错误信息:

While attempting to create blogcontrollerlist(alias: Blog\Controller\List) an invalid factory was registered for this instance type.

不确定这里出了什么问题,一切似乎都已就绪。

当文件位于
Blog\Factory\ListControllerFactory
中时,您在配置中指向
Blog\Controller\ListControllerFactory

像这样更新,它将工作:

'controllers' => array(
    'factories' => array(
        'Blog\Controller\List' => 'Blog\Factory\ListControllerFactory'
    )
)

@jkushner如果这个问题解决了,那么你可以接受答案,问题就结束了。。。