在symfony 2控制器中,每次我想从post中获取值时,我都需要运行:
$this->getRequest()->get('value1');
$this->getRequest()->get('value2');
有没有办法将它们合并到一个返回数组的语句中?类似于Zend的getParams()?
你可以做$this->getRequest()->query->all();
来获取所有GET参数,$this->getRequest()->request->all();
来获取所有POST参数。
所以在你的情况下:
$params = $this->getRequest()->request->all();
$params['value1'];
$params['value2'];
有关 Request 类的详细信息,请参阅 http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/Request.html
以最近的 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());
}
}
由于您位于控制器中,因此操作方法被赋予一个Request
参数。
您可以使用$request->request->all();
访问所有开机自检数据。这将返回一个键值对数组。
使用 GET 请求时,您可以使用 $request->query->all();
对于Symfony 3.4,你可以访问GET和POST的数据,喜欢这个
帖子:
$data = $this->request->request->all();
获取:
$data = $this->request->query->all();
好吧,请记住这始终是PHP,因此您可以检查超全局变量$_REQUEST
https://www.php.net/manual/en/reserved.variables.request.php