资源定义的方法Laravel-4上的MethodNotAllowedHttpException [英] MethodNotAllowedHttpException on resource defined method Laravel-4
问题描述
我创建了一个非常简单的表单,以便可以使用提交按钮而不是链接来打开编辑用户页面.使用链接可以完美地工作,但是即使方法("edit")在UsersController资源中已完美定义,并且正常工作,表单按钮也无法通过并产生MethodNotAllowedHttpException.
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::resource('users','UsersController');
UsersController:
UsersController:
public function edit($id)
{
$user = $this->user->find($id);
return View::make('users.edit')->with('user',$user);
}
show.blade.php:
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() }}
谢谢.
推荐答案
执行Form::open()
时,默认情况下使用post
请求方法.但是,当您创建Route::resource()
时,edit
方法将接受get
请求.
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')) }}
这篇关于资源定义的方法Laravel-4上的MethodNotAllowedHttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!