Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MethodNotAllowedHttpException laravel-4

Form :

{{ Form::open(array('url' => 'user/create', 'files' => true)) }}

Route :

Route::resource('user', 'UserController');

UserController.php

  class UserController extends BaseController {

    public function index()
    {
        return 'hi11';
        //return View::make('home.index');
    }
    public function create()
    {
        return 'hi22';
        //return View::make('home.index');
    }

}

This code gives
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

like image 349
bigData Avatar asked Jul 06 '13 09:07

bigData


3 Answers

I'd just like to add my own discovery along these lines... Maybe this will save someone else the head-scratching I just performed.

I too implemented the Route::resource mechanism. I couldn't figure out why my create was working but my update was not. It turns out you can't reuse the same form code exactly, the form that does an update must use the method PUT or PATCH. Why update couldn't be a POST is beyond me.

That is to say, the opening form tag for an update must look like this:

Form::model($thing, array(
    'method' => 'PUT', 
    'route' => array('things.update', $thing->id)
    )

Without specifying method => PUT, you get this not-helpful error.

like image 140
Barnabas Kendall Avatar answered Oct 10 '22 12:10

Barnabas Kendall


Because in your roures you use resourse controller, you can use only specific paths and actions, described in documentation http://laravel.com/docs/controllers#resource-controllers.

user/create ( UserController::create ) is where you need to show the form for adding a new user.

The actual storage of the user should be done in user/store i.e. your form must be send data to UserController::store() method.

In your case if you POST your form only to 'url' => 'user', this should automatically send data to the correct method.

like image 39
pgk Avatar answered Oct 10 '22 12:10

pgk


Laravel 4 resources have named routes - just use those:

{{ Form::open(array('route' => 'user.create', 'files' => true)) }}
like image 27
Laurence Avatar answered Oct 10 '22 11:10

Laurence