Search code examples
phpjsonbackbone.jslaravel-4

Laravel JSON reading from POST


Suppose I have a resourcefull route in Laravel 4.1, like next:

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

And I have controller method store() like next:

    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);

    }

I am making post request either from backbone, or just from rest api test tool: the request is following:

{"phone":"123123123","description":"","name":"test","email":"[email protected]"}

The problem is that when I look at the response I see that request was empty :

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

What can be the problem?


Solution

  • Set the following header in your request

    content-type:application/json
    

    or alternatively you can set it in the "App::before" filter

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