ZF2应用程序

时间:2015-10-22 16:01:23

标签: php zend-framework2

我有两家工厂。

第一个是控制器工厂:

    <?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);
        }
    }

第二个是Post ServiceFactory:

    <?php
    namespace Blog\Factory;

    use Blog\Service\PostService;
    use Zend\ServiceManager\FactoryInterface;
    use Zend\ServiceManager\ServiceLocatorInterface;

    class PostServiceFactory implements FactoryInterface
    {
         /**
          * Create service
          *
          * @param ServiceLocatorInterface $serviceLocator
          * @return mixed
          */
         public function createService(ServiceLocatorInterface $serviceLocator)
         {
             return new PostService(
                 $serviceLocator->get('Blog\Mapper\PostMapperInterface')
             );
         }
    }

这是我的模块配置:

    <?php
    return array(
        'service_manager' => array(
            'factories' => array(
                'Blog\Service\PostServiceInterface' => 'Blog\Factory\PostServiceFactory'
            )
        ),
        'controllers' => array(
            'factories' => array(
                'Blog\Controller\List' => 'Blog\Factory\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',
            ),
        )
    );

在控制器工厂,我必须针对getServiceLocator拨打ServiceLocatorInterface,然后拨打get电话。但是在邮政服务工厂,我只需拨打get。我做了一个转储,它看起来都是Zend\ServiceManager\ServiceManager类。当我尝试对邮政服务工厂服务定位器执行getServiceLocator调用时,它没有找到任何方法。

我不太了解最新情况?

1 个答案:

答案 0 :(得分:0)

控制器工厂的调用方式与临时服务工厂不同。传递给ServiceLocator的{​​{1}}实际上不是您要查找的createService,而是ServiceManager的实例。

如果您尝试这样做:

ControllerManager

你会得到输出:

public function createService(ServiceLocatorInterface $serviceLocator)
{
    $realServiceLocator = $serviceLocator->getServiceLocator();
    $postService = $realServiceLocator->get('Blog\Service\PostServiceInterface');

    var_dump(get_class($serviceLocator));

    return new ListController(postService );
}

虽然string(37) "Zend\Mvc\Controller\ControllerManager" 中的同一转储会给您:

PostServiceFactory

来自Zend 2文档:

http://framework.zend.com/manual/current/en/in-depth-guide/services-and-servicemanager.html#writing-a-factory-class

  

当使用将从string(34) "Zend\ServiceManager\ServiceManager" 调用的Factory-Class时,它实际上将自己注入ControllerManager。但是,我们需要真正的$serviceLocator才能访问我们的服务类。这就是我们调用函数ServiceManager的原因,它将为我们提供真实的getServiceLocator()