php 在 Symfony 2 中获取所有请求参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11227285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:01:21  来源:igfitidea点击:

Getting all request parameters in Symfony 2

phpsymfonyrequest

提问by ContextSwitch

In symfony 2 controllers, every time I want to get a value from post I need to run:

在 symfony 2 控制器中,每次我想从 post 获取值时,我都需要运行:

$this->getRequest()->get('value1');
$this->getRequest()->get('value2');

Is there any way to consolidate these into one statement that would return an array? Something like Zend's getParams()?

有没有办法将这些合并为一个返回数组的语句?Zend 的 getParams() 之类的东西?

回答by Guillaume Flandre

You can do $this->getRequest()->query->all();to get all GET params and $this->getRequest()->request->all();to get all POST params.

您可以$this->getRequest()->query->all();获取所有 GET 参数并$this->getRequest()->request->all();获取所有 POST 参数。

So in your case:

所以在你的情况下:

$params = $this->getRequest()->request->all();
$params['value1'];
$params['value2'];

For more info about the Request class, see http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/Request.html

有关 Request 类的更多信息,请参阅http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/Request.html

回答by Aftab Naveed

With Recent Symfony 2.6+ versions as a best practice Request is passed as an argument with action in that case you won't need to explicitly call $this->getRequest(), but rather call $request->request->all()

使用最近的 Symfony 2.6+ 版本作为最佳实践请求作为参数传递在这种情况下,您不需要显式调用 $this->getRequest(),而是调用 $request->request->all()

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
use Symfony\Component\HttpFoundation\RedirectResponse;

    class SampleController extends Controller
    {


        public function indexAction(Request $request) {

           var_dump($request->request->all());
        }

    }