C. Thubert
C. Thubert

Reputation: 79

Symfony 3, how to transfer URL parameters with redirectToRoute method

I use redirectToRoute in my controller, and i want to know how can i transfer URL parameters in this method (redirectToRoute) ?

I already search on the web (SF documentation and forums), but i didn't find the solution.

Thanks.

Upvotes: 5

Views: 5205

Answers (3)

Robert Saylor
Robert Saylor

Reputation: 1369

The newer way of doing arrays works as well.

    return $this->redirectToRoute('routename',[
        'param1' => $param1,
    ]);

The output will do a query-string.

routename?param1=data

Upvotes: 0

C. Thubert
C. Thubert

Reputation: 79

Sample of code :

// Get URL parameter :
$urlParam = $request->query->get('url-param') ;

// Transfer parameter
$urlParameters[ "url-param" ] = $urlParam ;
return $this->redirectToRoute("eqt_search", $urlParameters) ;

Upvotes: 0

Jakub Matczak
Jakub Matczak

Reputation: 15656

Let's look into source of this method:

/**
 * Returns a RedirectResponse to the given route with the given parameters.
 *
 * @param string $route      The name of the route
 * @param array  $parameters An array of parameters
 * @param int    $status     The status code to use for the Response
 *
 * @return RedirectResponse
 */
protected function redirectToRoute($route, array $parameters = array(), $status = 302)
{
    return $this->redirect($this->generateUrl($route, $parameters), $status);
}

As you can see the second argument is $parameters.

So simply pass them as an array as second argument. So it should be used like:

$this->redirectToRoute('show_page', array('id' => 123));

Upvotes: 8

Related Questions