【问题标题】:Implementing ServiceLocatorAwareInterface dependency for forward() class in a ZF2 service在 ZF2 服务中为 forward() 类实现 ServiceLocatorAwareInterface 依赖项
【发布时间】:2013-10-28 11:44:54
【问题描述】:

已编辑(代码已更新并适用于其他人)
了解正在发生的事情的总体思路。

我正在尝试从控制器中的视图访问发布数据,而不刷新页面。

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

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

我知道我必须实现 ServiceLocatorAwareInterface 才能使用 forward() 类,但我写的似乎不起作用。

            <?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() 类有依赖问题,但我不确定问题是什么。

编辑-
这是我从 viewHelper 调用 postAlbumService 的方式

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

在将依赖项注入服务后,有什么方法可以调用被请求服务中的特定类?

【问题讨论】:

  • 您的类postAlbumService 没有方法forward()。我不知道你为什么认为你可以调用那个方法,但是除非它被实现或继承,否则它是行不通的。你很可能想在不同的对象上调用同名的方法。

标签: php service dependencies zend-framework2 forward


【解决方案1】:

你做错了一些事情,你误解了一些事情......

首先,forward() 是一个ControllerPlugin。您将通过 ServiceLocator 访问所述管理器来访问此方法。一个例子可能是这样的:

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

现在,要将 ServiceLocator 放入您的任何服务类中,您需要依赖注入。其中一种方法是实现ServiceLocatorAwareInterface。 ZF2 的ServiceManager 有所谓的Listeners。这些监听器检查实现的接口和类似的东西。每当找到匹配项时,它都会通过给定函数的接口注入所需的依赖项。工作流程如下所示:

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

现在这告诉你什么。这告诉您,在您的任何类的__construct() 中,您所需的依赖项实际上都不存在。它们只有在类/服务被实例化后才会被注入。

最后一点,给定的代码示例并没有多大意义;)无论我想访问什么 ServiceAction,你总是让我回到“newAlbum”操作...

【讨论】:

  • 另外上面的代码只是为了测试我可以从 View->ViewHelper->Service->Controller 去,这就是为什么我还没有在服务中放任何东西:)
猜你喜欢
  • 2013-11-13
  • 2013-03-05
  • 1970-01-01
  • 2013-01-21
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多