本文介绍了Symfony2:在 Twig 中,通过 Render 函数传递请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用 {% render() %} 会自动强制发送一个新的请求对象,但我很好奇是否有办法将原始请求作为参数传递?>

I understand that using {% render() %} automatically forces a new request object to be sent, but im curious if theres a way to pass in the originating request as an argument?

{% render('some_action', {'originalRequest': app.request}) %}

这似乎对控制器没有任何作用:

This doesn't seem to do anything for the controller:

public function actionAction($originalRequest = null)
{
    // $originalRequest ends up just being null
}

我假设是因为路线设置的方式:

Im assuming its because of the way the route is setup:

some_action:
    pattern: /stuff/
    defaults: { _controller:SomeApp:Controller:action }

我想像这样的数据显然不能与 URL 分开,所以某种类型的方式将数据传递到可呈现的 URL,有什么吗?

I'd imagine data like that cant obviously be apart of the URL, so some type of way to pass in data to a renderable URL, anything at all?

编辑(解决方案)

从长远来看,解决方案非常简单,正如下面的 Petre Pătraşc 所证明的,在 Twig 中,我需要做的就是直接调用控制器,通过这种方法我可以传入对象(例如请求对象) 和数组,而不是 URL 中的文本值.
为了在控制器中执行大致相同的想法,利用路由器的 forward() 方法,将允许类似的效果,而无需将用户重定向到另一个页面.

The solution was pretty simple in the long run, as Petre Pătraşc below has demonstrated, that in Twig, all I needed to do was invoke the Controller directly, and with that approach I can pass in Objects (Such as a Request object) and Arrays, instead of text values in a URL.
To perform roughly the same idea in a controller, utilizing the forward() method from the router, will allow similar effects without needing to redirect the user to another page.

推荐答案

如果我理解正确,您正在寻找这个:

If I understand correctly, you're looking for this:

{% render "MyBundle:Controller:someAction" with { 'originalRequest' : app.request } %}

这篇关于Symfony2:在 Twig 中,通过 Render 函数传递请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:56