Laravel: POST -> Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49519097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:34:23  来源:igfitidea点击:

Laravel: POST -> Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

phplaravelpostcsrf

提问by zilijonas

fellow coders.

编码员。

Whenever I try to POST something, I get this error.

每当我尝试发布某些内容时,都会收到此错误。

enter image description here

在此处输入图片说明

create.blade.php file:

创建.blade.php 文件:

    <h1>Publish a Post</h1>

    <hr>

    <form method="POST" action="/posts">

      {{ csrf_field() }}

      <div class="form-group">
        <label for="title">Title</label>
        <input type="text" class="form-control" id="title" name="title">
      </div>

      <div class="form-group">
        <label for="body">Body</label>
        <textarea type="text" class="form-control" id="body" name="body"></textarea>
      </div>

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

    </form>

  </div>

web.php file:

web.php 文件:

Route::get('/', 'PostController@index');

Route::get('/posts/create', 'PostController@create');

Route::get('/posts', 'PostController@store');

PostController.php file:

PostController.php 文件:

  public function create()
    {
      return view('posts.create');
    }

    public function store()
    {
      dd(request()->all());
    }

And the database schema:

和数据库架构:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->timestamps();
    });
}

Any ideas what am I doing wrong or how to fix this issue would be really appreciated.

任何想法我做错了什么或如何解决这个问题将不胜感激。

回答by Chin Leung

You have no POSTdefined in your routes. Update your ::getto ::postfor your endpoint.

POST的路线中没有定义。更新您::get::post为您的端点。

Route::post('/posts', 'PostController@store');

For more information: https://laravel.com/docs/5.6/routing#basic-routing

更多信息:https: //laravel.com/docs/5.6/routing#basic-routing

回答by Boghani Chirag

you may register a resourceful route to the controller:

您可以向控制器注册一条资源丰富的路由:

Route::resource('/posts', 'PostController');

Route::resource('/posts', 'PostController');

For more information: https://laravel.com/docs/5.6/controllers

更多信息:https: //laravel.com/docs/5.6/controllers