【发布时间】: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