Zend framework2 如何将参数从视图助手(小部件)传递到操作?
我有一个小部件,它正在为另一个控制器调用一个操作,我需要向该操作传递一个参数。到目前为止,我尝试了以下操作,但出现以下错误: 错误 小部件 控制器: 从视野中呼唤 编辑1:尝试后:$id=$this->getEvent->getRouteMatch->getParam'id',0 活动: $this->getEvent->getRouteMatch返回空值 我正在尝试为我的用例修改此示例: 试试:Zend framework2 如何将参数从视图助手(小部件)传递到操作?,zend-framework2,zend-view,Zend Framework2,Zend View,我有一个小部件,它正在为另一个控制器调用一个操作,我需要向该操作传递一个参数。到目前为止,我尝试了以下操作,但出现以下错误: 错误 小部件 控制器: 从视野中呼唤 编辑1:尝试后:$id=$this->getEvent->getRouteMatch->getParam'id',0 活动: $this->getEvent->getRouteMatch返回空值 我正在尝试为我的用例修改此示例: 试试: $this->getEvent->getRouteMatch->getParam'id',0 实现
$this->getEvent->getRouteMatch->getParam'id',0 实现小部件有几种方法: 首先,在您尝试调用所需的操作时,将参数传递给您的操作(如$id),但这样,您的操作可能无法按预期工作,因为您正在创建操作的新入口点。你可以这样面对麻烦 我认为最好的方法是使用forward->dispatch controller插件,因为ZF2将获得所有上下文并隔离处理,从而为您提供一个ViewModel实例,您可以在之后进行渲染。你将获得更好的隔离 例如: 注意:您的管线定义必须是段类型,并且在管线模式中具有:id 控制器
public function editidentityinformationAction()
{
// use params form routing
$id = (int)$this->params()->fromRoute('id', 0);
// Some Logic using dispatch context, servicemanager, etc...
...
$view = new ViewModel(array(
'id' => $id,
'form' => $form,
));
$view->setTemplate('online-field-evaluation/tabs/editidentityinformation.phtml');
return $view;
}
查看帮助程序:
class IdentityInformationWidget extends AbstractHelper implements ServiceLocatorAwareInterface
{
protected $serviceLocator;
public function __construct(ServiceLocatorInterface $serviceLocator)
{
// $serviceLocator is an instance of ViewHelperManager
// We get ServiceManager (its "parent" ServiceLocatorInterface) with $serviceLocator->getServiceLocator()
// Make good use of your ServiceLocatorAwareInterface
$this->setServiceLocator($serviceLocator->getServiceLocator());
}
...
/**
* @param $id
*/
public function __invoke($id)
{
// Going through ServiceManager to get ControllerPluginManager and Forward controller plugin
$forwardPlugin = $this->getServiceLocator()
->get('ControllerPluginManager')->get('forward');
// Dispatch Action with parameter (Force ZF2 MVC dispatch stack process)
$viewModel = $this->forward()
->dispatch('OnlineFieldEvaluation\Controller\TabsController', array(
'action' => 'editidentityinformation',
'id' => $id
));
return $this->getView()->render($viewModel);
}
}
注意:我没有测试这段代码,当然有错误,但逻辑在这里
有关转发插件的更多帮助
通过这种方式,您可以将您的操作作为普通页面或在viewhelper的帮助下作为小部件,您的操作也是可测试的,因为它的逻辑不会作为网页或小部件更改,您还可以轻松测试小部件,因为您可以模拟服务等。谢谢,但仍然是相同的错误:致命错误:在第46行的C:\dev\xampp\htdocs\OnlineFieldEvaluation\module\OnlineFieldEvaluation\src\OnlineFieldEvaluation\Controller\TabsController.php中对非对象调用成员函数getParam
public function editidentityinformationAction()
{
$id = (int)$this->params()->fromRoute('id', 0);
//$id = (int)$this->params('id', 0);
...
$view = new ViewModel(array(
'id' => $id,
'form' => $form,
));
$view->setTemplate('online-field-evaluation/tabs/editidentityinformation.phtml');
return $view;
}
<?php echo $this->identityInformationWidget(3); ?>
public function editidentityinformationAction()
{
// use params form routing
$id = (int)$this->params()->fromRoute('id', 0);
// Some Logic using dispatch context, servicemanager, etc...
...
$view = new ViewModel(array(
'id' => $id,
'form' => $form,
));
$view->setTemplate('online-field-evaluation/tabs/editidentityinformation.phtml');
return $view;
}
class IdentityInformationWidget extends AbstractHelper implements ServiceLocatorAwareInterface
{
protected $serviceLocator;
public function __construct(ServiceLocatorInterface $serviceLocator)
{
// $serviceLocator is an instance of ViewHelperManager
// We get ServiceManager (its "parent" ServiceLocatorInterface) with $serviceLocator->getServiceLocator()
// Make good use of your ServiceLocatorAwareInterface
$this->setServiceLocator($serviceLocator->getServiceLocator());
}
...
/**
* @param $id
*/
public function __invoke($id)
{
// Going through ServiceManager to get ControllerPluginManager and Forward controller plugin
$forwardPlugin = $this->getServiceLocator()
->get('ControllerPluginManager')->get('forward');
// Dispatch Action with parameter (Force ZF2 MVC dispatch stack process)
$viewModel = $this->forward()
->dispatch('OnlineFieldEvaluation\Controller\TabsController', array(
'action' => 'editidentityinformation',
'id' => $id
));
return $this->getView()->render($viewModel);
}
}