Laravel 5 - POST上的methodnotallowedhttpexception错误

时间:2018-01-15 03:31:43

标签: php laravel-5.5

我正在创建一个基本的问题,以熟悉Laravel的功能,并且当我尝试"创建"我在提交时收到以下错误。

methodnotallowedhttpexception

知道为什么我会这样做吗?如果您需要任何其他代码剪辑,请与我们联系。

这是我的创建刀片模板。

<div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Add a Lead</h3>
      </div>
      <div class="panel-body">
        <div class="table-container">
          <form method="POST" action="{{ url('leads/create') }}"  role="form">
            {{ csrf_field() }}
            <div class="row">
              <div class="col-xs-6 col-sm-6 col-md-6">
                <div class="form-group">
                  <input type="text" name="first_name" id="first_name" class="form-control input-sm" placeholder="First Name">
                </div>
              </div>
              <div class="col-xs-6 col-sm-6 col-md-6">
                <div class="form-group">
                  <input type="text" name="last_name" id="last_name" class="form-control input-sm" placeholder="Last Name">
                </div>
              </div>
              <div class="col-xs-6 col-sm-6 col-md-6">
                <div class="form-group">
                  <input type="text" name="primary_phone" id="primary_phone" class="form-control input-sm" placeholder="Primary Phone #">
                </div>
              </div>
              <div class="col-xs-6 col-sm-6 col-md-6">
                <div class="form-group">
                  <input type="text" name="source" id="source" class="form-control input-sm" placeholder="Lead Source">
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-xs-12 col-sm-12 col-md-12">
                <input type="submit"  value="Save" class="btn btn-success btn-block">
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>

这是我的php artisan route:list

+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method    | URI                    | Name             | Action                                                                 | Middleware   |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD  | /                      |                  | Closure                                                                | web          |
|        | GET|HEAD  | api/user               |                  | Closure                                                                | api,auth:api |
|        | GET|HEAD  | home                   | home             | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | POST      | leads                  | leads.store      | App\Http\Controllers\LeadsController@store                             | web          |
|        | GET|HEAD  | leads                  | leads.index      | App\Http\Controllers\LeadsController@index                             | web          |
|        | GET|HEAD  | leads/create           |                  | Closure                                                                | web          |
|        | GET|HEAD  | leads/index            |                  | Closure                                                                | web          |
|        | PUT|PATCH | leads/{lead}           | leads.update     | App\Http\Controllers\LeadsController@update                            | web          |
|        | GET|HEAD  | leads/{lead}           | leads.show       | App\Http\Controllers\LeadsController@show                              | web          |
|        | DELETE    | leads/{lead}           | leads.destroy    | App\Http\Controllers\LeadsController@destroy                           | web          |
|        | GET|HEAD  | leads/{lead}/edit      | leads.edit       | App\Http\Controllers\LeadsController@edit                              | web          |
|        | POST      | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | GET|HEAD  | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST      | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | POST      | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest    |
|        | GET|HEAD  | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest    |
|        | POST      | password/reset         |                  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest    |
|        | GET|HEAD  | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
|        | GET|HEAD  | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
|        | POST      | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
|        | GET|HEAD  | splash                 |                  | Closure                                                                | web          |
|        | GET|HEAD  | users                  |                  | Closure                                                                | web          |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+

奖金问题:如何将auth作为中间件添加到我的潜在客户控制器中,以便用户必须登录才能查看?

我在web.php中定义的路线如下。

// Leads
Route::resource('leads','LeadsController');
Route::get('leads/index', function () { return view('leads.index'); });
Route::get('leads/create', function () { return view('leads.create'); });

2 个答案:

答案 0 :(得分:2)

您的表单提交了一个帖子请求,而您的leads/create路由已注册为GET路由。所以要么制作表单method="get",要么将您的路线注册为Route::post('leads/create', function(){ /* do something here*/ });

答案 1 :(得分:1)

表单的方法是POST请求,但是在您的路由中,您只有url'leaders / create'的get请求。

GET | HEAD |引导/创造

创建POST路由应该修复它。