Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: in Twig, pass Request with Render function

Tags:

twig

symfony

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 }

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?

EDIT (Solution)

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.

like image 659
RedactedProfile Avatar asked Oct 11 '13 20:10

RedactedProfile


2 Answers

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

{% render "MyBundle:Controller:someAction" with { 'originalRequest' : app.request } %}
like image 153
Petre Pătraşc Avatar answered Nov 09 '22 07:11

Petre Pătraşc


use the render function as a result

{{ render(controller('MyBundle:ControllerName:example', {'originalRequest': app.request})) }}

and then in your controller

public function exampleAction(Request $originalRequest)
{
    // do something
}
like image 35
Gorvely Tasinda Avatar answered Nov 09 '22 06:11

Gorvely Tasinda