Laravel 4 beta3 和 beta4 之间 Input::json() 函数的变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15820755/
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
Changes to Input::json() function between Laravel 4 beta3 and beta4
提问by Sameer
When I was developing in Laravel4 Beta3, I used to get JSON POST data from a service using Input::json() function, But when I updated to Laravel4 Beta4, I am getting following error:
当我在 Laravel4 Beta3 中开发时,我曾经使用 Input::json() 函数从服务中获取 JSON POST 数据,但是当我更新到 Laravel4 Beta4 时,出现以下错误:
Notice: Undefined property: Symfony\Component\HttpFoundation\ParameterBag::$productName in /Applications/MAMP/htdocs/commonDBAPI/app/controllers/UserController.php line 47
注意:未定义的属性:Symfony\Component\HttpFoundation\ParameterBag::$productName in /Applications/MAMP/htdocs/commonDBAPI/app/controllers/UserController.php line 47
Does any one have any idea, what could be the reason.
有没有人有任何想法,可能是什么原因。
Thanks,
谢谢,
回答by Phill Sparks
You can access just the JSON using Input::json()->all()
.
您可以仅使用 JSON 访问 JSON Input::json()->all()
。
JSON input is also merged into Input::all()
(and Input::get('key', 'default')
) so you can use the same interface to get Query string data, Form data and a JSON payload.
JSON 输入也合并到Input::all()
(and Input::get('key', 'default')
) 中,因此您可以使用相同的接口来获取查询字符串数据、表单数据和 JSON 负载。
The documentation does not yet reflect all changes because Laravel 4 is still in beta and the focus is on getting the code right, the documentation will be updated ready for the public release.
文档尚未反映所有更改,因为 Laravel 4 仍处于测试阶段,重点是使代码正确,文档将更新以准备公开发布。
How is JSON merged with Input::all()?
JSON 如何与 Input::all() 合并?
Consider the following JSON:
考虑以下 JSON:
{
'name': 'Phill Sparks',
'location': 'England',
'skills': [
'PHP',
'MySQL',
'Laravel'
],
'jobs': [
{
'org': 'Laravel',
'role': 'Quality Team',
'since': 2012
}
]
}
When merged into Laravel's input the JSON is decoded, and the top-level keys become top-level keys in the input. For example:
当合并到 Laravel 的输入中时,JSON 被解码,顶级键成为输入中的顶级键。例如:
Input::get('name'); // string
Input::get('skills'); // array
Input::get('jobs.0'); // object
Input::all(); // Full structure of JSON, plus other input
回答by Juni Samos De Espinosa
Yup they changed it to return a ParameterBag object switch your code to Input::json()->all()
是的,他们将其更改为返回一个 ParameterBag 对象将您的代码切换为 Input::json()->all()
回答by Obez
For : { "name":"Olivier", "title":"Just a try" }
对于 : { "name":"Olivier", "title":"Just a try" }
Try this :
尝试这个 :
$input = Input::json()->all();
$input = Input::json()->all();
return $input['name'];
返回 $input['name'];