MethodNotAllowedHttpException on resource defined

2019-09-06 03:10发布

I created a very simple form so that I could use a submit button rather than a link to open up an edit users page. Using a link works perfectly, but the form button fails and yields a MethodNotAllowedHttpException even though the method ("edit") is perfectly defined in the UsersController resource and otherwise works fine.

Route:

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

UsersController:

public function edit($id)
    {
        $user = $this->user->find($id);
        return View::make('users.edit')->with('user',$user);
    }

show.blade.php:

<!-- This works fine: -->
{{ link_to_route('users.edit', ("Edit: " .$user->first_name." ".$user->last_name), $user->id) }}

<!-- This doesn't work, and yields the Method Not Allowed exception: -->
{{ Form::open(array('route' => array('users.edit',$user->id))) }}
{{ Form::submit('Edit User', array('class'=>'button')) }}
{{ Form::close() }}

Thanks.

2条回答
Summer. ? 凉城
2楼-- · 2019-09-06 04:03

When you do Form::open(), it defaults to using the post request method. But when you create a Route::resource(), the edit method takes a get request.

To make it work through the form, you'll need to open it with an additional parameter, like this:

{{ Form::open(array('route' => array('users.edit',$user->id),
   'method' => 'get')) }}
查看更多
时光不老,我们不散
3楼-- · 2019-09-06 04:11

You need to point to the update route, not edit.

{{ Form::open(array('route' => array('users.update', $user->id))) }}

The edit route is for displaying the view, while the update is for the put/patch request.

For more information about using the RESTful routes, I'd recommend checking out http://laravel.com/docs/controllers#resource-controllers

查看更多
登录 后发表回答