ZF2:如何将参数传递给转发插件,然后我可以在将它们转发到的方法中获取这些参数?

编程入门 行业动态 更新时间:2024-10-28 22:32:09
本文介绍了ZF2:如何将参数传递给转发插件,然后我可以在将它们转发到的方法中获取这些参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在 Foo 控制器中有一个需要参数的 Action 方法:

I have an Action method in Foo Controller which requires parameters:

public function fooAction($one, $two) {
    $a = one;
    $b = $two;
}

而且我需要从某个 Boo 控制器的另一个方法转发到该方法.这些参数之一必须是通过引用参数.手册 是这样的:

And I need to forward to that method from the other method of some Boo Controller. And one of those parameters has to be by reference parameter. The only example that the manual has is this:

$result = $this->forward()->dispatch('Boo\Controller\Boo', array('action' => 'boo'));

没有任何附加参数.但他们写道:

No any additional parameters. But they write:

$params 是一个可选的参数数组,用于查看用于此特定请求的 RouteMatch 对象.

$params is an optional array of parameters with which to see a RouteMatch object for purposes of this specific request.

所以,我尝试了:

$result = $this->forward()->dispatch('Boo\Controller\Boo', array(
                                                      'action' => 'boo',
                                                      'one' => &$one,
                                                      'two' => $two,
                                                 ));

但它不起作用.

有没有办法将额外的参数传递给转发控制器?

Is there any way to pass additional parameters to forward controller?

UPD:

这些也不起作用:

$result = $this->forward()->dispatch('Boo\Controller\Boo', array(
                                                      'action' => 'boo',
                                                      'params' => array(
                                                          'one' => &$one,
                                                          'two' => $two,
                                                     )));


$result = $this->forward()->dispatch('Boo\Controller\Boo', array(
                                                      'action' => 'boo',
                                                      'options' => array(
                                                          'one' => &$one,
                                                          'two' => $two,
                                                     )));

UPD 2:

我仍然无法获得我想要的功能(使用 forward 插件传递参数),但我找到了其他解决方案.在调用 forward 插件之前,我将变量设置为 Request 对象,在 forward 之后,我从 Request 获取它们> 在我的 boo 我的 Boo\Controller\BooController 的操作中:

I still can't get the functionality I want (to pass parameters with the forward plugin) but I found other solutions. Before calling the forward plugin I set the variables to the Request object and after the forward I get them from the Request in my boo Action of my Boo\Controller\BooController:

// in Foo::fooAction
$this->getRequest()->one = &$one;
$this->getRequest()->two = $two;
$result = $this->forward()->dispatch('Boo\Controller\Boo', array('action' => 'boo'));

// in Boo::booAction
$a = $this->getRequest()->one;
$b = $this->getRequest()->two;

愚蠢的解决方案,它不适用于 Ajax 请求.仍然对如何使用 forward 插件传递参数感兴趣.或者可能是如何在 booAction 中获取它们.因为如果我使用 forward 传递它们,Request 中没有任何内容.

Stupid solution, it will not work with Ajax requests. Still interested how to pass parameters with the forward plugin. OR MAYBE how to get them in the booAction. Because there in no anything in the Request if I pass them with the forward.

UPD 3 和决赛:

我终于找到了他们决定隐藏我通过 forward 插件传递的参数的地方.他们将它们放在 RouteMatch 对象中.

I finally found where they've decided to hide parameters I pass with the forward plugin. They put them in the RouteMatch object.

- 试着猜猜我们把你的参数藏在哪里了......哦,是的,它们在 RouteMatch 中,当然它们在那里,你不觉得别的吗?

- Tryyyy to guess where we've hidden your params... Oh yeeah, they are in the RouteMatch, of course they are there, didn't you think smth else?

手册的 forward 插件部分中没有任何信息!

And NO ANY info in the forward plugin section of the manual!

要获取参数,我必须在我的 BooController::booAction 中执行此操作:

To get params, I have to do this in my BooController::booAction:

$param = $this->getEvent()->getRouteMatch()->getParam('nameOfParam');

推荐答案

您可以创建一个容器类并在两个控制器中使用它

You can create a container class and use it in both controllers

在 module.conf 中

in module.conf

public function getServiceConfig()
{
    return array(
        'invokables' => array(
            'my_handy_container'        => 'path\container_class_name',
        )
    );
}

在两个控制器中创建一个 getter:

Create a getter in both controllers:

public function getMyHandyContainer()
{
    if (!$this->myHandyContainer) {
        $this->myHandyContainer = $this->getServiceLocator()->get('my_handy_container');
    }
    return $this->myHandyContainer;
}

并使用:

$myContainer = $this->getMyHandyContainer()->myHandyContainer;
$myContainer->foo = 5; // set something

ZF2 使用 forward 传递变量的方法

在传递方法中做:

ZF2 way to pass vars using forward

In the passing method do:

return $this->forward()->dispatch('controller_name', [
    'action' => 'whatever',
    'varname' => $value,
    'varname2' => $value2
]);

在调用的控制器方法中,执行:

In the invoked controller method, do:

$param2 = $this->params()->fromRoute('varname2',false);

这篇关于ZF2:如何将参数传递给转发插件,然后我可以在将它们转发到的方法中获取这些参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 13:37:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1042344.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   如何将   插件   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!