[英]MethodNotAllowedHttpException appearing and I've tried everything
I'm trying check the access control of /post/10/delete but it keeps on giving this error "MethodNotAllowedHttpException" 我正在尝试检查/ post / 10 / delete的访问控制,但它不断显示此错误“ MethodNotAllowedHttpException”
THIS IS MY VIEW 这是我的看法
@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
THIS IS THE ROUTE 这是路线
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');
AND THIS IS THE POSTSCONTROLLER 这就是后置控制器
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');
//
}
it keeps on giving an error "MethodNotAllowedHttpException" when I insert the link /post/5/delete 当我插入链接/ post / 5 / delete时,它会不断给出错误“ MethodNotAllowedHttpException”
You're having an incorrect URL 您输入的网址不正确
<form method="POST" action="/post/{{$posts->id}}/delete">
@csrf
@method('DELETE')
<button class="btn btn-default" type="submit">Delete Post</button>
</form>
Also either remove this line from your routes or the other lines related to PostsController
because they're being duplicated Run php artisan route:list
to check for yourself 也可以从您的路线中删除此行或与
PostsController
相关的其他行,因为它们被重复运行php artisan route:list
检查自己
Route::resource('post','PostsController'); // <--- Delete this
Now if you chose to delete this one line instead change your route accordingly 现在,如果您选择删除这一行,请相应地更改路线
Route::delete('/post/{id}/delete', 'PostsController@destroy');
@method('DELETE')
in the blade file instruct the form to send a request with the DELETE
method signature which is a RESTful API HTTP request convention when dealing with resources 刀片文件中的
@method('DELETE')
指示表单发送带有DELETE
方法签名的请求,该签名是在处理资源时的RESTful API HTTP请求约定
Learn More 学到更多
The Route::resource
alone generates RESOURCEFUL routes for you including almost everything you need 仅
Route::resource
为您生成RESOURCEFUL路线,包括您几乎需要的所有东西
Here's an example 这是一个例子
routes/web.php
<?php
Route::resource('post', 'PostsController');
Run php artisan route:list -c
运行
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 |
+-----------+-------------------------------+---------------------------------------------------------------+
You get yourself named generated urls with method signature with 1 line 您可以使用方法签名和1行来命名自己生成的url
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.