如何在 Symfony 2 中获取请求参数? [英] How to get the request parameters in Symfony 2?

查看:34
本文介绍了如何在 Symfony 2 中获取请求参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 symfony 很陌生.在其他语言,如 java 和其他语言中,我可以使用 request.getParameter('parmeter name') 来获取值.

I am very new to symfony. In other languages like java and others I can use request.getParameter('parmeter name') to get the value.

我们可以用 symfony2 做类似的事情吗.
我看过一些例子,但没有一个对我有用.假设我有一个名为 username 的表单域.在表单操作中,我尝试使用以下内容:

Is there anything similar that we can do with symfony2.
I have seen some examples but none is working for me. Suppose I have a form field with the name username. In the form action I tried to use something like this:

$request = $this->getRequest();
$username= $request->request->get('username'); 

我也试过

$username = $request->getParameter('username');

$username=$request->request->getParameter('username');

但没有一个选项有效.但是以下工作正常:

But none of the options is working.However following worked fine:

foreach($request->request->all() as $req){
    print_r($req['username']);
}

我在使用 getParameter() 方法时做错了什么.任何帮助将不胜感激.

Where am I doing wrong in using getParameter() method. Any help will be appreciated.

推荐答案

命名并不是那么直观:

use Symfony\Component\HttpFoundation\Request;

public function updateAction(Request $request)
{
    // $_GET parameters
    $request->query->get('name');

    // $_POST parameters
    $request->request->get('name');

这篇关于如何在 Symfony 2 中获取请求参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆