Laravel JSON从POST读取

时间:2013-12-23 10:37:34

标签: php json backbone.js laravel-4

假设我在Laravel 4.1中有一个资源丰富的路由,就像下一个:

Route::group(['prefix' => 'api',], function()
{
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Headers: Content-Type');
    Route::resource('user','UserController');
});

我有下一个控制器方法store()

    public function store()
    {
        $user = new User;
        $name = Input::get('name');
        $user->login_name = implode('.', explode(' ', strtolower($name)));
        $user->name = $name;
        $user->email = Input::get('email');
        $user->password = Hash::make('1');
//      $user->save();
        return Response::json([
                'error' => false,
                'user' => $user->toArray()
                ], 200);

    }

我正在从骨干网或者从rest api测试工具发出请求:请求如下:

{"phone":"123123123","description":"","name":"test","email":"test@test.com"}

问题是,当我看到响应时,我看到请求是空的:

{"error":false,"user":{"login_name":"","name":null,"email":null}}

可能是什么问题?

1 个答案:

答案 0 :(得分:3)

在请求中设置以下标题

content-type:application/json

或者你也可以在“App :: before”过滤器中设置它

App::before(function($request)
{
    $request->headers->set('content-type','application/json');
});