【问题标题】:Laravel: POST -> Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpExceptionLaravel: POST -> Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
【发布时间】:2018-09-06 05:30:03
【问题描述】:

各位程序员。

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

create.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 文件:

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

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

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

PostController.php 文件:

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

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

以及数据库架构:

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

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

【问题讨论】:

    标签: php laravel post csrf


    【解决方案1】:

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

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

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

    【讨论】:

    • 非常感谢并为我的愚蠢感到抱歉。解决了我的问题!
    • @LiJonas 没问题,祝你有美好的一天! :)
    【解决方案2】:

    你可以注册一个资源丰富的路由到控制器:

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

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

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 2019-07-01
      • 2019-01-06
      • 2021-06-10
      • 1970-01-01
      • 2014-11-20
      • 2019-04-11
      • 2018-06-21
      • 2018-03-25
      相关资源
      最近更新 更多