Laravel 4 beta3和beta4之间对Input :: json()函数的更改

Sam*_*eer 8 laravel laravel-4

当我在Laravel4 Beta3中开发时,我曾经使用Input :: json()函数从服务获取JSON POST数据,但是当我更新到Laravel4 Beta4时,我收到以下错误:

注意:未定义的属性:/Applications/MAMP/htdocs/commonDBAPI/app/controllers/UserController.php第47行中的Symfony\Component\HttpFoundation\ParameterBag :: $ productName

有没有人有任何想法,可能是什么原因.

谢谢,

Phi*_*rks 15

您可以使用JSON访问Input::json()->all().

JSON输入也合并到Input::all()(和Input::get('key', 'default'))中,因此您可以使用相同的接口来获取Query字符串数据,Form数据和JSON有效负载.

该文档尚未反映所有更改,因为Laravel 4仍处于测试阶段且重点在于使代码正确,文档将更新为公开发布做好准备.


JSON如何与Input :: all()合并?

考虑以下JSON:

{
    'name': 'Phill Sparks',
    'location': 'England',
    'skills': [
        'PHP',
        'MySQL',
        'Laravel'
    ],
    'jobs': [
        {
            'org': 'Laravel',
            'role': 'Quality Team',
            'since': 2012
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

当合并到Laravel的输入时,JSON被解码,顶级键成为输入中的顶级键.例如:

Input::get('name'); // string
Input::get('skills'); // array
Input::get('jobs.0'); // object
Input::all(); // Full structure of JSON, plus other input
Run Code Online (Sandbox Code Playgroud)

  • "Input :: all()"不包含JSON的唯一原因是,如果您没有使用有效负载发送JSON内容类型.见[isJson()](https://github.com/laravel/framework/blob/master/src/Illuminate/Http/Request.php#L425).`Input :: json()`假定您已发送JSON并且不检查内容类型. (2认同)