如何在ZF3中访问控制器内的服务定位器对象?

时间:2017-07-30 06:58:38

标签: php zend-framework2 zend-framework3

我正在尝试访问控制器内的服务定位器对象,但无法执行此操作 我尝试过在线帮助,但大多数人都在关注ZF2

以前在zf2访问Servicelocator是轻而易举的,我只需要做这个 - >> getServiceLocator();

我已尝试为我的控制器创建工厂类,并在那里创建了createService方法,但它说我也必须实现__invoke()方法。

我的目标是做这样的事情

public function getPropertyTable()
{
    if (!$this->PropertyTable) {
        $sm = $this->getServiceLocator();
        $this->PropertyTable = $sm->get('Application\Model\PropertyTable');
    }
    return $this->PropertyTable;
}


任何人都可以为我提供完整的步骤吗?

在问这个问题之前,我曾试图实现几乎所有与Servicelocator相关的答案,所以请在将此标记为重复之前帮助我,忽略错别字

4 个答案:

答案 0 :(得分:3)

谢谢大家告诉我,我做错了。
关于这个主题的更多研究帮助我解决了我的问题。这就是我为解决这个问题所做的工作
    

  •         为您的控制器创建工厂类
            您必须为控制器创建一个工厂类,它将实现zend的FactoryInterface。         在那里你必须打电话给

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
     {
           return new ListController($container->get(PropertyInterface::class));
     }
    


            这里我传递的是PropertyInterface引用,它是由我的另一个名为Property Table的表实现的,其中我已经为我的所有接口函数提供了body用于模型
            喜欢searchProperty()     

  •         在控制器的配置文件module.config.php中添加工厂类
            指示配置文件让我们工厂为控制器创建对象

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


        

  •  
  •         在配置文件中注册您的模型
           您必须为服务管理器添加新部分,并在那里提供您的模型类。

    // Add this section:
            'service_manager' => [
                'aliases' => [
                    Model\PropertyInterface::class => Model\PropertyTable::class,
                ],
                'factories' => [
                    Model\PropertyTable::class => InvokableFactory::class,
                ],
            ],
    


        


  • 现在只剩下你的PropertyInterface中的函数和PropertyTable中的Implentation,然后在你的控制器中调用它们

    Complete Steps for implementation 帮助我实施新流程。
    感谢社区。你们都是最好的。

    答案 1 :(得分:2)

    只要ZF3中的新工厂界面是:

    interface FactoryInterface
    {
        /**
         * Create an object
         *
         * @param  ContainerInterface $container
         * @param  string             $requestedName
         * @param  null|array         $options
         * @return object
         */
        public function __invoke(ContainerInterface $container, $requestedName, array $options = null);
    }
    

    你必须像这样实施你的工厂。

    为了帮助您,您可以使用link

    编辑:

    在实践中你应该有这个:

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $propertyTable = $container->get('Application\Model\PropertyTable');
        return new Controller($propertyTable);
    }
    

    答案 2 :(得分:0)

    另一种方式

    /**
     * Retrieve service manager instance
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @return ContainerInterface
     */
    public function getServiceLocator()
    {
        return $this->getEvent()->getApplication()->getServiceManager();
    }
    
    

    答案 3 :(得分:-1)

    在ZF3中,不建议将服务定位器传递给控制器​​。 您需要从控制器工厂内的$ container获取所有依赖项,并通过构造函数(或setter)将它们传递给控制器​​。

    相关问题