Support the ongoing development of Laravel.io →
posted 10 years ago
Requests

Hi, I'm using laravel 4 with backbone and it has been working fine up till now.

I'm trying to update my model in the database. Backbone fires a PUT request but I get the error

"NetworkError: 500 Internal Server Error - http://localhost:8000/users"

And in the response I get

"error":{"type":"Symfony\\Component\\HttpKernel\\Exception\\MethodNotAllowedHttpException","message":"","file":"\/var\/www\/app_testing\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php","line":1439}}

The requests GET and POST work fine but PUT is giving this error:

here is my backbone code:

App.Views.Register = Backbone.View.extend({
    className: 'container',
    template: template('registerTemplate'),

	    events: {
   	    'submit': 'register'
    },

    initialize: function() {
	    this.render();
    },

    render: function() {
	    this.$el.html( this.template() );
	    return this;
    },

    register: function(e) {
	    e.preventDefault();

	    App.user.save({
		    'email'		: this.$('form').find('#email').val(),
		    'name' 		: this.$('form').find('#name').val(),
		    'password' 	: this.$('form').find('#password').val()
	    });

	    console.log(App.user);
    }
});

And here is my resource controller:

    <?php

class UsersController extends \BaseController {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
	//
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
	//
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
	$newUser = Input::all();
	$newUser['ipaddress'] = Request::getClientIp();
	// $user = json_encode($newUser);
	
	return UsersModel::create($newUser);
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
	//
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
	//
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
	return "updated";
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
	//
}

And here is my route:

Route::resource('users' 	   , 'UsersController');

Please someone help. This is urgent

Last updated 2 years ago.
0

after running

php artisan routes

I get

+--------+---------------------------------------+----------------------+--------------------------------+----------------+---------------+
| Domain | URI                                   | Name                 | Action                         | Before Filters | After Filters |
+--------+---------------------------------------+----------------------+--------------------------------+----------------+---------------+
|        | GET /users                            | users.index          | UsersController@index          |                |               |
|        | GET /users/create                     | users.create         | UsersController@create         |                |               |
|        | POST /users                           | users.store          | UsersController@store          |                |               |
|        | GET /users/{users}                    | users.show           | UsersController@show           |                |               |
|        | GET /users/{users}/edit               | users.edit           | UsersController@edit           |                |               |
|        | PUT /users/{users}                    | users.update         | UsersController@update         |                |               |
|        | PATCH /users/{users}                  |                      | UsersController@update         |                |               |
|        | DELETE /users/{users}                 | users.destroy        | UsersController@destroy        |                |               |
+--------+---------------------------------------+----------------------+--------------------------------+----------------+---------------+

and my backbone model is

App.Models.User = Backbone.Model.extend({

	idAttribute : '_id',
	url			: '/users',

	validate: function(attrs){
		//Do some Validation
	}
});
Last updated 2 years ago.
0

The problem was in my model...

I had the url property set to

url: '/users'

while saving the model I had to do

App.user.save({
    'email'		: this.$('form').find('#email').val(),
    'name' 		: this.$('form').find('#name').val(),
    'password' 	: this.$('form').find('#password').val()
}, { url: '/users/' + App.user.get('_id') });

and specify the url for the PUT request

Last updated 2 years ago.
0

Try putting this line in to your backbone script

Backbone.emulateHTTP = true 

http://backbonejs.org/#Sync-emulateHTTP

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Haseeb90 haseeb90 Joined 8 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.