该路由不支持POST方法。支持的方法:GET,HEAD,PATCH,DELETE

时间:2020-04-22 10:12:46

标签: php laravel bootstrap-4

我试图理解为什么提交表单时出现此错误。我已经找到了解决方法,但是我不一定理解为什么需要这种解决方法,以及以后是否会引起问题。我发布此消息不仅是为了了解错误,而且还可以帮助遇到类似问题的其他任何人。

下面的粗体变通方法文字是我为获得此功能所做的工作,添加了“ / posts / create”。但是,在Laravel docs中,存储路径显示为“ / post”,“ PostController @ store”。

Web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Mail\WelcomeMail;

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

Route::get('/email', function () {
    return new WelcomeMail();
});

Auth::routes();

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

Route::get('/contact/create', 'ContactController@create');
Route::post('/contact', 'ContactController@store');

Route::get('/posts', 'PostController@index');
Route::get('/posts/create', 'PostController@create');
Route::post('/posts/create', 'PostController@store'); **<-workaround**
Route::get('/posts/{post}', 'PostController@show');
Route::get('/posts/{post}/edit', 'PostController@edit');
Route::patch('/posts/{post}', 'PostController@update');
Route::delete('/posts/{post}', 'PostController@destroy');

表格

<div class="container">
    <div class="row justify-content-cemter">
       <div class="col-md-8">
           <div class="card">
               <div class="card-header">Create New Post</div>


                <div class="card-body">
                    <form action="/posts/create" method="post">
                        @csrf

                        <div class="form-group">
                            <label for="title">Title</label>
                            <input name="title" type="text" class="form-control" id="title" aria-describedby="titleHelp" placeholder="Enter Title" autocomplete="off" value="{{ old('title') ?? $post->title }}">
                            <small id="titleHelp" class="form-text text-muted">Give your post a title that will describe your post easily</small>

                            @error ('title') <p style="color:red;">{{ $message }}</p> 
                            @enderror
                        </div>

                        <div class="form-group">
                            <label for="body">Body</label>
                            <input name="body" type="text" class="form-control" id="body" aria-describedby="bodyHelp" placeholder="Enter Post Body" autocomplete="off" value="{{ old('body') ?? $post->body }}">
                             <small id="bodyHelp" class="form-text text-muted">Enter as much detail you'd like!</small>

                             @error ('body') <p style="color:red;">{{ $message }}</p> 
                             @enderror
                        </div>

                        <button type="submit" class="btn btn-primary">Create Post</button>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

PostContorller

<?php

namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $posts = Post::all();

        return view('post.index', compact('posts'));
    }

    public function create()
    {
        $post = new Post();

        return view('post.create', compact ('post'));
    }

    public function store()
    {
        Post::create($this->validatedData());

        return redirect('/posts');
    }

    public function show(Post $post)
    {
        return view('post.show', compact('post'));
    }

    public function edit(Post $post)
    {
        return view('post.edit', compact('post'));
    }

    public function update(Post $post)
    {
        $post->update($this->validatedData());

        return \redirect('/posts');
    }

    public function destroy(Post $post)
    {
        $post->delete();

        return redirect('/posts');
    }

    protected function validatedData()
    {
        return request()->validate([
            'title' => 'required',
            'body' => 'required'
            //'photograph'=> nullable
            //'gif'=> nullable
        ]);
    }
}

Route:list

| Domain | Method   | URI                    | Name             | Action                                                                 | Middleware   |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD | /                      |                  | Closure                                                                | web          |
|        | GET|HEAD | about                  |                  | Closure                                                                | web          |
|        | GET|HEAD | api/user               |                  | Closure                                                                | api,auth:api |
|        | POST     | contact                |                  | App\Http\Controllers\ContactController@store                           | web          |
|        | GET|HEAD | contact/create         |                  | App\Http\Controllers\ContactController@create                          | web          |
|        | GET|HEAD | email                  |                  | Closure                                                                | web          |
|        | GET|HEAD | home                   | home             | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | GET|HEAD | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST     | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | POST     | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | GET|HEAD | password/confirm       | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    | web,auth     |
|        | POST     | password/confirm       |                  | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            | web,auth     |
|        | POST     | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web          |
|        | POST     | password/reset         | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web          |
|        | GET|HEAD | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web          |
|        | GET|HEAD | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web          |
|        | GET|HEAD | posts                  |                  | App\Http\Controllers\PostController@index                              | web,auth     |
|        | GET|HEAD | posts/create           |                  | App\Http\Controllers\PostController@create                             | web,auth     |
|        | POST     | posts/create           |                  | App\Http\Controllers\PostController@store                              | web,auth     |
|        | GET|HEAD | posts/{post}           |                  | App\Http\Controllers\PostController@show                               | web,auth     |
|        | PATCH    | posts/{post}           |                  | App\Http\Controllers\PostController@update                             | web,auth     |
|        | DELETE   | posts/{post}           |                  | App\Http\Controllers\PostController@destroy                            | web,auth     |
|        | GET|HEAD | posts/{post}/edit      |                  | App\Http\Controllers\PostController@edit                               | web,auth     |
|        | POST     | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
|        | GET|HEAD | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+

3 个答案:

答案 0 :(得分:2)

我怀疑您在某处存在路线冲突,这就是为什么您会收到该错误的原因。您发布的代码仅显示您的变通办法,这使得很难知道导致错误的原始代码中发生了什么。 我看不到您的解决方法会导致任何问题。

答案 1 :(得分:1)

在刀片视图中添加以下内容:@method('PUT')进行商店致电

                <div class="card-body">
                    <form action="/posts/create" method="post">
                        @csrf 
                        @method('PUT')

在旁注中,如果您感到困惑,可以运行以下artisan命令以查看每种路线所需的方法:php artisan route:list

答案 2 :(得分:0)

在表单视图中使用@method('PUT')。 HTML不支持PUT操作,您正在使用@method('PUT')欺骗它来存储必要的信息。请查看:Laravel's Routing以获得更多说明。

注意:同样,您遇到此问题的原因是因为您两次使用相同的路径,即

Route::get('/posts/create', 'PostController@create');
Route::post('/posts/create', 'PostController@store'); **<-workaround**

在这两种情况下都使用'/ posts / create'给您带来错误。请更改为Route::post('/posts', 'PostController@store');