我试图在程序中创建一个新的“Airborne”测试,但收到 405 MethodNotAllowed 异常。
路线
Route::post('/testing/{id}/airbornes/create', [
'uses' => 'AirborneController@create'
]);
Controller
public function create(Request $request, $id)
{
$airborne = new Airborne;
$newairborne = $airborne->newAirborne($request, $id);
return redirect('/testing/' . $id . '/airbornes/' . $newairborne)->with(['id' => $id, 'airborneid' => $newairborne]);
}
查看
<form class="sisform" role="form" method="POST" href="{{ URL::to('AirborneController@create', $id) }}">
{{ csrf_field() }}
{!! Form::token(); !!}
<button type="submit" name="submit" value="submit" class="btn btn-success">
<i class="fas fa-plus fa-sm"></i> Create
</button>
</form>
最佳答案
据我所知,表单没有 href 属性。我认为您应该写 Action 但写了 href。 请在您尝试提交的表单中指定action属性。
<form method="<POST or GET>" action="<to which URL you want to submit the form>">
就你的情况而言
<form method="POST" ></form>
并且缺少操作属性。如果操作属性缺失或设置为“”(空字符串),则表单将提交给自身(相同的 URL)。
例如,您已将表单显示的路由定义为
Route::get('/airbornes/show', [
'uses' => 'AirborneController@show'
'as' => 'airborne.show'
]);
然后您提交一个没有操作属性的表单。它将把表单提交到当前所在的同一路由,并查找具有相同路由的 post 方法,但您没有具有 POST 方法的相同路由。所以你会得到 MethodNotAllowed 异常。
要么使用 post 方法定义相同的路由,要么显式指定 HTML 表单标记的 action 属性。
假设您有一个定义如下的路由来将表单提交到
Route::post('/airbornes/create', [
'uses' => 'AirborneController@create'
'as' => 'airborne.create'
]);
所以你的表单标签应该像
<form method="POST" action="{{ route('airborne.create') }}">
//your HTML here
</form>
关于php - Laravel 异常 405 方法不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53674329/