且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Symfony2:如何将url查询字符串参数传递给控制器​​?

更新时间:2023-11-19 21:20:22

要在扩展Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller的控制器中使用GET/POST参数:

To work with GET / POST parameters in a controller that extends Symfony\Bundle\FrameworkBundle\Controller\Controller:

public function updateAction()
{
    $request = $this->getRequest();
    $request->query->get('myParam'); // get a $_GET parameter
    $request->request->get('myParam'); // get a $_POST parameter
    ...
}

对于不扩展Symfony基本控制器的控制器,请将请求对象声明为action方法的参数,然后按上述步骤进行操作:

For a controller which does not extend the Symfony base controller, declare the request object as a parameter of the action method and proceed as above:

public function updateAction(Request $request)
{
    $request->query->get('myParam'); // get a $_GET parameter
    $request->request->get('myParam'); // get a $_POST parameter
    ...
}