2017-07-30 84 views
2

我想访问我的控制器内的服务定位器对象,但无法做到这一点。
我试图联机帮助,但他们大多正在跟踪ZF2

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

以前在zf2服务定位器访问方式是一件轻而易举的事,我只是做$这个 - > getServiceLocator();

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

我的目标是做这样的事情

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


谁能给我一个完整的步骤来实现这一目标?

我试图问这个问题之前,实现几乎所有涉及到的ServiceLocator的答案,所以请帮我标志着这个作为复制或某件事之前,忽略了错别字

+0

[Zend Framework 3中的GetServiceLocator]的可能重复(https://stackoverflow.com/questions/42974794/getservicelocator-in-zend-framework-3) – akond

+2

在ZF2中,确定在控制器中使用Service locator是一个非常非常糟糕坏坏的做法。这是不可能/建议在ZF3中再次使用该技巧 – Hooli

+1

@Hooli谢谢,我完全理解并同意您的观点,我知道这就是为什么ZF3中服务经理的整个流程发生了变化的原因。但这就是我提出这个问题的原因,我不知道如何在控制器内部调用我的模型的函数。请看我是一位正在学习这项技术的新手。我希望如果你能给我一个例子,我怎样才能在Controller中调用模型函数。 –

回答

3

谢谢大家告诉我我做错了。
关于这个主题的一些更多的研究,帮我把我的问题解决了
这里是我做了什么来解决它

  • 为控制器
    创建工厂类你必须为你的控制器工厂类将实现Zend的FactoryInterface。 在那里你必须调用

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


    这里我传递这是由我称为房产表另一个表中,我已经给身体我所有inteface功能模型
    像searchProperty实施PropertyInterface refrence ()

  • 加工厂类在配置文件中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, 
          ], 
         ], 
    



  • 现在唯一剩下的就是在PropertyTable添加功能在你的PropertyInterface和Implentation他们,然后叫他们在你的控制器

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

    +0

    很高兴你终于解决了你的问题,如果我的答案帮助你解决了你的问题,upvote可以很好的方式... – Hooli

    1

    只要在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); 
    } 
    
    +1

    感谢您指点我正确的方向, 我在网上查找有关您的答案,并发现**这个** [链接](https://docs.zendframework.com/tutorials/in-depth-guide/models -and-servicemanager /#writing-a-factory-class) 我从这个链接实现了代码,在这里取得了相当的成功。只有跟随此代码的问题是现在我无法访问我的sql查询的适配器对象。 你能帮我一下,在我的PropertyTable模型中,我该如何注入Zend \ Db \ Adapter \ Adapter类对象。所以我可以像这样使用它: '$ sql = new Sql($ this-> adapter);' –

    +1

    @PankajJha编辑 – Hooli

    -1

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

    +0

    你能告诉我一个使用setter的例子吗?或任何链接 – Athar

    +0

    @Athar只需在这里或谷歌搜索中输入“php依赖注入设置器”。关于这个话题有很多文章。 –