Laravel PutNot操作上的MethodNotAllowedHttpException

时间:2019-01-21 19:29:44

标签: laravel

我从laravel开始,尝试尝试使用旧版本(5.2)的教程。 因此,我尝试使用laravel的最新版本进行翻译。

在编辑表单时出现此错误:

  

symfony \组件\ HttpKernel \异常\   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

我尝试添加放置在两部分中,因为我发现了很多与此问题有关的消息,但并不能解决问题。

我的路线列表很好,并且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    |
+--------+-----------+------------------------+------------------+----------------------------------------------------

有人有主意吗?

3 个答案:

答案 0 :(得分:0)

我认为

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

应该是

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

$centres是对的吗?如果没有,那么将$centres更改为Form(centres->id)中的'route'=> ['centres.update', $centres->id]之类的东西,也许是主要原因。

答案 1 :(得分:0)

pip的形式输入。在这里您可以找到有关https://laravel.com/docs/5.7/csrf

的更多信息

答案 2 :(得分:0)

我找到了解决方案,但我认为这不是很好。 如果我在模板中写下该表格,就可以了,并且没有错误。

<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

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