【问题标题】:Laravel MethodNotAllowedHttpException on put actionLaravel MethodNotAllowedHttpException on put 动作
【发布时间】:2019-06-15 05:40:11
【问题描述】:

我从 laravel 开始并尝试在旧版本(5.2)上做一个教程。 所以我尝试使用最新版本的 laravel 进行翻译。

我在编辑表单时遇到此错误:

symfony\Component\HttpKernel\Exception\ MethodNotAllowedHttpException 无消息

我在 web.php 上的路线

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('centres', 'CentresController');

我的控制器是 Centrescontroller.php

<?php

namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\http\Controllers\Controller;
use App\centre;

class CentresController extends Controller
{



  public function index()
  {
      $centres = Centre::get();
     return view('centres.index', compact('centres'));
  }

  public function edit($id)
  {
     $centres = Centre::find($id);
    /// dd($centres);
    return view('centres.edit', compact('centres'));
  }

  public function update($id, Request $request)
  {
      return "Utilisateur modifié !";

  }
}

我的带有编辑表单的模板是 /views/centres/edit.balde.php

@extends('layouts.app')

@section('content')
<h1>Editer</h1>
{!! Form::open(['url' => route('centres.update', $centres), 'method' => 'PUT' ]) !!}

    {{ method_field('PUT') }}

    {!! Form::text('titre', 'titre',  $centres->titre) !!}

    {!! Form::text('slug', 'slug',  $centres->slug) !!}

    {!! Form::textarea('description', 'description',  $centres->descriptif) !!}

    {!!Form::submit("Envoyer")!!}


{!! Form::close() !!}

@endsection

我尝试在两个部分中添加 put,因为我发现了很多关于这个问题的消息,但它并没有解决问题。

我的路线列表很好,centers.update 肯定没问题,所以我不明白这个问题:

λ php artisan route:list
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method    | URI                    | Name             | Action                                                                 | Middleware   |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD  | /                      |                  | Closure                                                                | web          |
|        | GET|HEAD  | api/user               |                  | Closure                                                                | api,auth:api |
|        | POST      | centres                | centres.store    | App\Http\Controllers\CentresController@store                           | web          |
|        | GET|HEAD  | centres                | centres.index    | App\Http\Controllers\CentresController@index                           | web          |
|        | GET|HEAD  | centres/create         | centres.create   | App\Http\Controllers\CentresController@create                          | web          |
|        | PUT|PATCH | centres/{centre}       | centres.update   | App\Http\Controllers\CentresController@update                          | web          |
|        | GET|HEAD  | centres/{centre}       | centres.show     | App\Http\Controllers\CentresController@show                            | web          |
|        | DELETE    | centres/{centre}       | centres.destroy  | App\Http\Controllers\CentresController@destroy                         | web          |
|        | GET|HEAD  | centres/{centre}/edit  | centres.edit     | App\Http\Controllers\CentresController@edit                            | web          |
|        | GET|HEAD  | home                   | home             | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | 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         | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest    |
|        | GET|HEAD  | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
|        | POST      | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
|        | GET|HEAD  | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
+--------+-----------+------------------------+------------------+----------------------------------------------------

有人有想法吗?

【问题讨论】:

  • 你尝试过表单模型绑定吗?我没有尝试在更新时使用 Form::open,但我认为这可能与它有关。考虑以下代码:{!! Form::model($centres, ['method' =&gt; 'PATCH', 'route' =&gt; 'centres.update']) !!}
  • 我试过了,还有更多的语法,但不可能。我一直有 MethodNotAllowedHttpException
  • 不要将您的解决方案编辑到您的问题中。相反,将其作为答案发布。

标签: laravel


【解决方案1】:

我找到了解决方案,但我认为它不是很好。 如果我把它写在我的模板中,表格是好的,我没有错误。

<form method="post" action="{{ route('centres.update', $centres->id) }}">


{!!  method_field('PUT') !!}
        {{ csrf_field() }}

        {!! Form::text('titre', 'titre',  $centres->titre) !!}

        {!! Form::text('slug', 'slug',  $centres->slug) !!}

        {!! Form::textarea('description', 'description',  $centres->descriptif) !!}

        {!!Form::submit("Envoyer")!!}


</form>

        @endsection

但我认为这不是好的解决方案吗?

【讨论】:

    【解决方案2】:

    把它写在@csrf 的表格上。在这里你可以找到更多关于它的信息https://laravel.com/docs/5.7/csrf

    【讨论】:

      【解决方案3】:

      我觉得

      {!! Form::open(['url' => route('centres.update', $centres), 'method' => 'PUT' ]) !!}
      

      应该是

       {{ Form::open(['method' => 'put',  'route'=> ['centres.update', $centres], 'class'=>'form']) }}
      

      $centres 是 id 对吗?如果不是,则将 $centres 更改为 Form ( 'route'=&gt; ['centres.update', $centres-&gt;id] ) 中的 centres-&gt;id 之类的东西,这可能是主要原因。

      【讨论】:

      • 没有 $centre 是主要对象。我试过你的解决方案,但同样的问题。我认为这是路线上的问题,但我不明白为什么我无法访问它
      • @christophe 那么为什么要以形式传递整个对象?它应该只是id$centres-&gt;id
      • 它在我的 5.2 版本中。它说可以发送对象并且框架检测到ID。我用 $centre->id 更新但我有同样的问题。
      • 你可以尝试将{{ method_field('PUT') }} 更改为{!! method_field('PUT') !!}@method('PUT')
      • 我认为您在表单中也缺少{{ csrf_field() }}
      猜你喜欢
      • 2016-05-27
      • 2019-01-06
      • 2016-06-23
      • 2015-11-18
      • 1970-01-01
      • 2019-04-15
      • 2013-07-04
      • 2015-09-11
      • 2017-12-21
      相关资源
      最近更新 更多