Laravel 405异常 [英] Laravel Exception 405 MethodNotAllowed

查看:905
本文介绍了Laravel 405异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在程序中创建一个新的空降"测试,并得到405 MethodNotAllowed异常.

I'm trying to create a new "Airborne" test in my program and getting a 405 MethodNotAllowed Exception.

路线

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

查看

<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 . 请在您要提交的表单中指定操作属性.

According to my knowledge forms don't have a href attribute. I think you suppose to write Action but wrote href. Please specify action attribute in the form that you are trying to submit.

<form method="<POST or GET>" action="<to which URL you want to submit the form>">

在您的情况下为

<form method="POST" ></form>

并且缺少动作属性.如果缺少动作属性或将其设置为"(空字符串),则表单将提交给自己(相同的URL).

And action attribute is missing. If action attribute is missing or set to ""(Empty String), the form submits to itself (the same URL).

例如,您已定义了将表单显示为的路径

For example, you have defined the route to display the form as

Route::get('/airbornes/show', [
    'uses' => 'AirborneController@show'
    'as' => 'airborne.show'
]);

,然后提交不带动作属性的表单.它将把表单提交到当前所在的相同路径,并且它将寻找具有相同路径的post方法,但是使用POST方法则没有相同的路径.因此,您将获得MethodNotAllowed异常.

and then you submit a form without action attribute. It will submit the form to same route on which it currently is and it will look for post method with the same route but you dont have a same route with a POST method. so you are getting MethodNotAllowed Exception.

要么使用post方法定义相同的路由,要么显式指定HTML表单标签的action属性.

Either define the same route with post method or explicitly specify your action attribute of HTML form tag.

假设您的路线定义如下,将表单提交给

Let's say you have a route defined as following to submit the form to

Route::post('/airbornes/create', [
        'uses' => 'AirborneController@create'
        'as' => 'airborne.create'
    ]);

因此您的表单标签应类似于

So your form tag should be like

<form method="POST" action="{{ route('airborne.create') }}">
//your HTML here
</form>

这篇关于Laravel 405异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆