是否可以将数据转发到 Zend 2 中的另一个控制器/操作? [英] Is it possible to forward data to another controller/action in Zend 2?

查看:50
本文介绍了是否可以将数据转发到 Zend 2 中的另一个控制器/操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为一个常见的问题是试图将数据转发(发布)到另一个页面.通常,我会使用会话在页面之间传递数据,但是 Zend 中的这个前向助手看起来很有潜力.有没有办法获取有关转发请求的信息?就像要求转发器一样(如果没有转发器,它通常会返回 null)?

I think a common problem is trying to forward (post) data to another page. Normally, I'd resort to sessions to pass data between pages, but this forward helper in Zend looks like it has potential. Is there any way to get information about a forward request? Like ask for the forwarder (and it'd return null normally when there's no forwarder)?

如果没有当前的实现,有可能吗?这将是一个有趣的项目,无论如何我一直想要的东西.(我目前也在使用我自己的 BcryptDbTableAuth 类,直到找到更好的解决方案).

And if there's no current implementation, is it possible? It'd be a fun project, and something I've wanted forever anyways. (I'm also currently using my own BcryptDbTableAuth class until I find a better solution).

顺便说一下,我不是在谈论添加请求参数.它应该对用户不可见.而且我仍在研究可变路线(通配符应该可以解决问题,但我不断收到路线无法匹配"......会再深入研究一下)

By the way, I'm not talking about adding request params. It should be invisible to the user. And I'm still investigating variable routes (Wildcard is supposed to do the trick but I keep getting "route cannot be matched"... will drill on that a bit more)

推荐答案

转发"有两个选项.理解 php 是作为服务器端语言的处理器,用于获取传入的请求并返回响应.

There are two options to "forward". Understand php is as server side language a processor to grab an incoming request and return a response.

也就是说,第一个转发"使用框架内转发.这意味着只有一个请求和一个响应.框架在内部调用一个控制器操作,然后再调用另一个.Zend Framework 调用此方法forward.

That said, the first "forward" uses in-framework forwarding. This means there is only one request and one response. Internally the framework calls one controller action and then another one. Zend Framework calls this method forward.

第二个转发"是真正的重定向,其中第一个响应包含 Location 标头和 302 http 状态代码.这会导致第二个请求并连续出现第二个响应.Zend Framework 调用此方法重定向.

The second "forward" is a real redirect, where the first response contains a Location header and the 302 http status code. This results in a second request and consecutively in a second response. Zend Framework calls this method redirect.

所以在上面,您在问题中谈到的 forward 不涉及任何会话或路由匹配参数,因为对操作的第二次调用在同一个 php 进程中,所以所有变量都是已经知道了.

So with above, the forward you talk about in your question does not involve any sessions or route match parameters, since the second call to an action is within the same php process, so all variables are already known.

正向示例

转发的一个例子是使用forward控制器插件:

An example to forward is to use the forward controller plugin:

class MyController
{
  public function myAction()
  {
    return $this->forward()->dispatch('MyModule\Controller\Other', array(
      'action' => 'other',
      'foo'   => 'bar',
      'baz'   => new Bat()
    ));
  }
}

访问:

class OtherController {
    public function otherAction()
    {
        $foo = $this->params()->fromRoute('foo');
    }
}

您可能会注意到,可以向前向调用添加其他参数,包括真实对象.

As you might note, it is possible to add additional parameters to the forward call including real objects.

重定向示例

一种选择是使用路由参数,以便您在发回的 url 中捕获数据.因为你说你不想要那样,所以你必须为此使用会话:

One option is to use the route parameters, so you capture data in the url you send back. Because you say you don't want that, you have to use a session for that:

use Zend\Session\Container;

class MyController
{
  public function myAction()
  {
    $session = new Container('MyContainer');
    $session->foo = 'bar';
    $session->baz = 'bat';

    return $this->redirect()->toRoute('my/other/route');
  }
}

这篇关于是否可以将数据转发到 Zend 2 中的另一个控制器/操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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