php 如何在symfony2中获取请求参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9784930/
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
How to get the request parameters in symfony2
提问by Abdul Hamid
I am very new to symfony. In other languages like java and others I can use request.getParameter('parmeter name')
to get the value.
我对 symfony 很陌生。在其他语言(如 java 和其他语言)中,我可以使用它request.getParameter('parmeter name')
来获取值。
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:
我们可以用 symfony2 做类似的事情吗?
我看过一些例子,但没有一个对我有用。假设我有一个名为username的表单字段。在表单操作中,我尝试使用以下内容:
$request = $this->getRequest();
$username= $request->request->get('username');
I have also tried
我也试过
$username = $request->getParameter('username');
$username = $request->getParameter('username');
and
和
$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']);
}
Where am I doing wrong in using getParameter()
method. Any help will be appreciated.
我在使用getParameter()
方法时做错了什么。任何帮助将不胜感激。
回答by Cerad
The naming is not all that intuitive:
命名并不是那么直观:
use Symfony\Component\HttpFoundation\Request;
public function updateAction(Request $request)
{
// $_GET parameters
$request->query->get('name');
// $_POST parameters
$request->request->get('name');
回答by pkout
I do it even simpler:
我做的更简单:
use Symfony\Component\HttpFoundation\Request;
public function updateAction(Request $request)
{
$foo = $request->get('foo');
$bar = $request->get('bar');
}
Another option is to introduce your parameters into your action function definition:
另一种选择是将参数引入到动作函数定义中:
use Symfony\Component\HttpFoundation\Request;
public function updateAction(Request $request, $foo, $bar)
{
echo $foo;
echo $bar;
}
which, then assumes that you defined {foo} and {bar} as part of your URL pattern in your routing.yml file:
然后假设您在 routing.yml 文件中将 {foo} 和 {bar} 定义为 URL 模式的一部分:
acme_myurl:
pattern: /acme/news/{foo}/{bar}
defaults: { _controller: AcmeBundle:Default:getnews }
回答by ScoRpion
You can Use The following code to get your form field values
您可以使用以下代码获取表单字段值
use Symfony\Component\HttpFoundation\Request;
public function updateAction(Request $request)
{
// retrieve GET and POST variables respectively
$request->query->get('foo');
$request->request->get('bar', 'default value if bar does not exist');
}
Or You can also get all the form values as array by using
或者您也可以通过使用将所有表单值作为数组获取
$request->request->all()
回答by MartinW.
try
尝试
$request->request->get('acme_demobundle_usertype')['username']
inspect attribute name of your formular field
检查公式字段的属性名称
回答by Reinherd
Inside a controller:
在控制器内部:
$request = $this->getRequest();
$username = $request->get('username');
回答by luchaninov
Your options:
您的选择:
- Simple:
$request->request->get('param')
($_POST['param']
) or$request->query->get('param')
($_GET['param']
)
- Good Symfony forms with all validation, value transormation and form rendering with errors and many other features:
- Something in between (see example below)
- 简单的:
$request->request->get('param')
($_POST['param']
) 或$request->query->get('param')
($_GET['param']
)
- 良好的 Symfony 表单,具有所有验证、值转换和带有错误的表单渲染以及许多其他功能:
- 介于两者之间(见下面的例子)
<?php
/**
* @Route("/customers", name="customers")
*
* @param Request $request
* @return Response
*/
public function index(Request $request)
{
$optionsResolver = new OptionsResolver();
$optionsResolver->setDefaults([
'email' => '',
'phone' => '',
]);
$filter = $optionsResolver->resolve($request->query->all());
/** @var CustomerRepository $customerRepository */
$customerRepository = $this->getDoctrine()->getRepository('AppBundle:Customer');
/** @var Customer[] $customers */
$customers = $customerRepository->findFilteredCustomers($filter);
return $this->render(':customers:index.html.twig', [
'customers' => $customers,
'filter' => $filter,
]);
}
More about OptionsResolver
- http://symfony.com/doc/current/components/options_resolver.html
更多关于OptionsResolver
- http://symfony.com/doc/current/components/options_resolver.html
回答by Ashish Awasthi
As now $this->getRequest()
method is deprecated you need to inject Request
object into your controller action like this:
由于现在$this->getRequest()
方法已被弃用,您需要Request
像这样将对象注入到控制器操作中:
public function someAction(Request $request)
after that you can use one of the following.
之后,您可以使用以下方法之一。
If you want to fetch POST data from request use following:
如果您想从请求中获取 POST 数据,请使用以下内容:
$request->request->get('var_name');
but if you want to fetch GET data from request use this:
但如果你想从请求中获取 GET 数据,请使用:
$request->query->get('var_name');
回答by Yoander
You can do it this:
你可以这样做:
$clientName = $request->request->get('appbundle_client')['clientName'];
Sometimes, when the attributes are protected, you can not have access to get the value for the common method of access:
有时,当属性受到保护时,您无法获取常用访问方法的值:
(POST)
(邮政)
$clientName = $request->request->get('clientName');
(GET)
(得到)
$clientName = $request->query->get('clientName');
(GENERIC)
(通用的)
$clientName = $request->get('clientName');
回答by Antonis Charalambous
Most of the cases like getting query string or form parameters are covered in answers above.
上面的答案涵盖了大多数情况,例如获取查询字符串或表单参数。
When working with raw data, like a raw JSON string in the body that you would like to give as an argument to json_decode()
, the method Request::getContent()
can be used.
处理原始数据时,例如您想作为参数提供给 的正文中的原始 JSON 字符串,可以使用json_decode()
该方法Request::getContent()
。
$content = $request->getContent();
Additional useful informations on HTTP requests in Symfony can be found on the HttpFoundationpackage's documentation.
可以在HttpFoundation包的文档中找到有关 Symfony 中 HTTP 请求的其他有用信息。
回答by Meet
use Symfony\Component\HttpFoundation\Request;
public function indexAction(Request $request, $id) {
$post = $request->request->all();
$request->request->get('username');
}
Thanks , you can also use above code
谢谢,你也可以使用上面的代码