MethodNotAllowedHttpException出现,我已经尝试了一切

时间:2019-09-04 19:19:28

标签: php html laravel

我正在尝试检查/ post / 10 / delete的访问控制,但它始终显示此错误“ MethodNotAllowedHttpException”

这是我的看法


@section('content')

<div>
<h1>{{$posts->title}}</h1>
<h3>{!!$posts->body!!}</h3>
<hr>
<small>created on {{$posts->created_at}}</small>
<hr>
@if(!Auth::guest())
    @if(Auth::user()->id==$posts->user_id)
<a href="/post/{{$posts->id}}/edit" class="btn btn-default">Edit</a>
<hr>
<form method="POST" action="{{$posts->id}}/delete">
        {{ csrf_field() }}
    <button class="btn btn-default" type="submit">Delete Post</button>
</form>
    @endif
@endif

</div>

@endsection

这就是路线

Route::get('/services','PagesController@services');
Route::get('/about','PagesController@about');


Route::post('/save','PostsController@store');

Route::post('/post/{id}/update', 'PostsController@update');

Route::post('/post/{id}/delete', 'PostsController@destroy');

Route::resource('post','PostsController');

Auth::routes();

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

这就是邮递员

public function destroy($id)
    {
        $post = Post::find($id);
        if(auth()->user()->id !== $post->user_id)
         {  return redirect('/post')->with('error','Unauthorized Access');}

        $post->delete();
        return redirect('/post')->with('success','Post Deleted');
        //
    }

当我插入链接/ post / 5 / delete时,它会不断显示错误消息“ MethodNotAllowedHttpException”

1 个答案:

答案 0 :(得分:0)

您输入的网址不正确

<form method="POST" action="/post/{{$posts->id}}/delete">
     @csrf
     @method('DELETE')
    <button class="btn btn-default" type="submit">Delete Post</button>
</form>

也请从您的路线中删除此行或与PostsController相关的其他行,因为它们已重复 运行php artisan route:list来检查自己

Route::resource('post','PostsController'); // <--- Delete this

现在,如果您选择删除这一行,而是相应地更改路线

Route::delete('/post/{id}/delete', 'PostsController@destroy');
刀片文件中的

@method('DELETE')指示表单发送带有DELETE方法签名的请求,该签名是处理资源时的RESTful API HTTP请求约定。
Learn More

编辑:

Route::resource即可为您生成 RESOURCEFUL 路线,包括几乎所有您需要的路线

这是一个例子

routes/web.php

<?php
Route::resource('post', 'PostsController');

运行php artisan route:list -c
Output

+-----------+-------------------------------+---------------------------------------------------------------+
| Method    | URI                           | Action                                                        |
+-----------+-------------------------------+---------------------------------------------------------------+
| GET|HEAD  | post                          | App\Http\Controllers\PostsController@index                    |
| POST      | post                          | App\Http\Controllers\PostsController@store                    |
| GET|HEAD  | post/create                   | App\Http\Controllers\PostsController@create                   |
| GET|HEAD  | post/{post}                   | App\Http\Controllers\PostsController@show                     |
| PUT|PATCH | post/{post}                   | App\Http\Controllers\PostsController@update                   |
| DELETE    | post/{post}                   | App\Http\Controllers\PostsController@destroy                  |
| GET|HEAD  | post/{post}/edit              | App\Http\Controllers\PostsController@edit                     |
+-----------+-------------------------------+---------------------------------------------------------------+

您将使用方法签名(1行)来命名自己生成的url