Symfony 3,如何使用redirectToRoute方法传输URL参数

5
我在控制器中使用了redirectToRoute方法,我想知道如何在这个方法中传递URL参数?我已经在网上查找了(包括SF文档和论坛),但是没有找到解决方法。谢谢。
3个回答

8

让我们来看一下这个方法的源代码:

/**
 * 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);
}

正如您所看到的,第二个参数是$parameters

因此,只需将它们作为第二个参数传递一个数组即可。因此,应该像这样使用:

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

0

使用新的数组方式同样有效。

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

输出将执行一个查询字符串。

routename?param1=data


0

代码示例:

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接