简体   繁体   English

如何在Symfony 2.8中访问特定帖子字段的值

[英]How to access value of a particular post field in symfony 2.8

I want to access a particular field value when posted, similar to $_POST['title'] in core PHP. 发布后,我想访问特定的字段值,类似于核心PHP中的$ _POST ['title']。 How can I achieve this in Symfony 2.8? 如何在Symfony 2.8中实现此目标?

So far I have tried using 到目前为止,我已经尝试使用

$request->request->get('name') --- returns all values from post. $request->request->get('name') ---返回post中的所有值。

$request->request->all() --- same returns all values. $request->request->all() --- same返回所有值。

$request->request->get($form->get('name')) -- again same..returns all values $request->request->get($form->get('name')) -同样..返回所有值

If I pass something in get() it shows nulls, eg:- If I passed $request->request->get('Title') --- returns null. 如果我在get()中传递某些内容,则显示为空, eg:-如果我通过$request->request->get('Title') ---返回null。

I have attached the image for better understanding. 我已附上图片以更好地理解。

Returns All 返回全部

Returns Blank 返回空白

Controller 调节器

You can do like this : 您可以这样:

$request->query->get('Title');
// OR
$request->request->get('Title');
// OR
$request->get('Title');

You can also able to this with Entity : 您也可以使用Entity来做到这一点:

$article = new Article();
$article->getTitle();

Full Information : https://symfony.com/doc/current/introduction/http_fundamentals.html#symfony-request-object 完整信息: https : //symfony.com/doc/current/introduction/http_fundamentals.html#symfony-request-object

I solved it by storing it into a variable, like this 我通过将其存储到变量中来解决它,就像这样

$data = $request->request->get('form');

//or //要么

$data = $request->request->all();

and then accessing it through array keys. 然后通过数组键访问它。 $data['Title'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM