8

How to render a controller with get parameters in twig?

{{ render(controller('AutoBundle:MyController:main', { 'id': id, 'active': true } )) }}

to call controller like this: https://example.com/users/323?active=true

3
  • This code snippet should work, what do you intend to do exactly ? Commented Aug 24, 2015 at 8:33
  • you need to get this parameters (id and active) from the current request? Commented Aug 24, 2015 at 8:34
  • $request->query->get('active') -> "NULL"... weird :S Commented Aug 24, 2015 at 8:39

2 Answers 2

17

In accordion with the doc the query argument is the third parameters.

{{ controller(controller, attributes, query) }}

So try this:

{{ render(controller('AutoBundle:MyController:main', {}, { 'id': id, 'active': true } )) }}

Hope this help

Sign up to request clarification or add additional context in comments.

Comments

2

Yes, it doesn't send the parameters as a GET request, but seems to call the method directly.

Twig template part:

<div id="question-choose-semester">
    {{ render(controller('UnswCamsBundle:Semester:choiceAjax', { 'redirect':  path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params'))} )) }}
</div>

Create an optional parameter. If using annotations to define the route, you don't have to add it to the annotation specification.

  public function choiceAjaxAction($redirect = "", Request $request) {

Then check the GET parameter:

if (empty($redirect)) {
  $redirect_uri = $request->query->get('Redirect');
} else {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.