Laravel 异常 405 方法不允许

2024-01-29

我试图在我的程序中创建一个新的“Airborne”测试并收到 405 MethodNotAllowed 异常。

Routes

Route::post('/testing/{id}/airbornes/create', [
    'uses' => 'AirborneController@create'
]);

控制器

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]);
}

View

<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laravel 异常 405 方法不允许 的相关文章

随机推荐