在ZF2服务中实现forward()类的ServiceLocatorAwareInterface依赖关系 [英] Implementing ServiceLocatorAwareInterface dependency for forward() class in a ZF2 service

查看:331
本文介绍了在ZF2服务中实现forward()类的ServiceLocatorAwareInterface依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDITED(代码更新并为其他人工作)

有关发生的情况的整体想法。

EDITED (Code is updated and working for others)
For the overall idea of what's happening.

我正在尝试访问帖子来自控制器视图的数据,而不刷新页面。



为此,我使用 ViewHelper 来执行页面控制器,调用下面的服务,然后转发回控制器;之后我可以管理页面控制器中发布的数据。

I'm trying to access post data from the view in the controller, without refreshing the page.

To do this I am executing the page controller by using a ViewHelper to call the Service below which then forwards back to the controller; afterwards I can manage the posted data in the page controller.

除了最后一步, forward()以外,一切正常,我收到错误Call to undefined method AlbumModule\Service\postAlbumService :: forward()

Everything works except the last step which is the forward(), I receive the error Call to undefined method AlbumModule\Service\postAlbumService::forward()

我明白我必须实现 ServiceLocatorAwareInterface 为了使用 forward()类,但我写的似乎不起作用。

I understand I must implement the ServiceLocatorAwareInterface in order to use the forward() class, but what I've written doesn't seem to work.

            <?php
            namespace AlbumModule\Service;

            use Zend\ServiceManager\ServiceLocatorAwareInterface;
            use Zend\ServiceManager\ServiceLocatorInterface;

            class postAlbumService implements
                ServiceLocatorAwareInterface
            {
                protected $services;

                public function __construct() {
                    echo '<script>console.log("postAlbumService is Started")</script>';
                }

                public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
                {
                    $this->services = $serviceLocator;
                }

                public function getServiceLocator()
                {
                    return $this->services;
                }

                public function test(){
                    $cpm = $this->getServiceLocator()->get('controllerpluginmanager');
                    $fwd = $cpm->get('forward');
                    echo '<script>console.log("postAlbumService TEST() is Started")</script>';
                    return $fwd->dispatch('newAlbum', array('action' => 'submitAlbum'));
                }
            }

好像我只是一个依赖问题与forward()类,但我不知道是什么问题。

It seems as though I'm just having a dependency issue with the forward() class, but I'm not sure what the issue is.

EDIT-

这是我如何呼叫 postAlbumService from viewHelper

EDIT-
Here is how I am calling the postAlbumService from the viewHelper

            <?php
            namespace AlbumModule\View\Helper;

            use Zend\View\Helper\AbstractHelper;

            class invokeIndexAction extends AbstractHelper
            {
             protected $sm;

                   public function test()
                    {
                        $this->sm->getServiceLocator()->get('AlbumModule\Service\postAlbumService')->test();
                    }

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

有没有办法调用一个特定的类服务被请求后,依赖关系被注入到服务中?

Is there any way to call a specific class in the service being requested, after the dependencies are injected into the service?

推荐答案

你正在做一些事情错误,重新误解一些事情...

You're doing a couple of things wrong and you're misunderstanding some things...

首先, forward()是一个 ControllerPlugin < STRONG>。您可以通过 ServiceLocator 访问该管理员来访问此方法。一个例子可以是:

First of all, forward() is a ControllerPlugin. You'll gain access to this method by accessing said manager via the ServiceLocator. An example could be this:

$cpm = $serviceLocator->get('controllerpluginmanager');
$fwd = $cpm->get('forward');
return $fwd->dispatch('foo/bar');

现在,要将 ServiceLocator 需要依赖注入。其中一种方法是实现 ServiceLocatorAwareInterface 。 ZF2的 ServiceManager 已经调用了侦听器。这些侦听器检查实现的接口和这样的东西。每当找到匹配项时,它将通过接口给出的函数来注入所需的依赖关系。工作流程如下所示:

Now, to get the ServiceLocator into any of your Service-Classes you need Dependency Injection. One of the ways is to implement the ServiceLocatorAwareInterface. The ServiceManager of ZF2 has so called Listeners. These Listeners check for implemented interfaces and stuff like this. Whenever it finds a match, it injects the required dependencies via the interfaces given functions. The workflow looks like this:

ServiceManager get('FooBar');
    $ret = new FooBar();
    foreach (Listener) 
        if $ret instanceof Listener
             doInjectDependenciesInto($ret)
        end
    end
    return $ret

这是什么告诉你的。这告诉你,你的任何类中的$ code> __ construct()实际上是你所需要的依赖关系。他们只有在类/服务被实例化之后才被注入。

Now what does this tell you. This tells you, that within the __construct() of any of your classes NONE of your required dependencies are actually there. They only get injected AFTER the class/service has been instantiated.

在最后一个备注中,给定的代码示例并不真正有意义)我想要访问什么ServiceAction,你会永远回到我的newAlbum动作...

On a last side-note, the given code example doesn't really make much sense ;) No matter what ServiceAction i'd like to access, you'd always return me to the "newAlbum" action...

这篇关于在ZF2服务中实现forward()类的ServiceLocatorAwareInterface依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆