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

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

问题描述

我对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');

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

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