【问题标题】:Laravel 5.2 MethodNotAllowedHttpExceptionLaravel 5.2 MethodNotAllowedHttpException
【发布时间】:2018-01-13 08:50:42
【问题描述】:

我的 Laravel 5.2 项目在添加“添加评论”部分时遇到 MethodNotAllowedHttpException 错误。

这是我的路线:

Route::post('/posts/{post}/comments', 'CommentsController@store');

这是我的评论控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use App\Comment;
use App\Http\Requests;

class CommentsController extends Controller
{
    public function store(Post $post)
    {
        Comment::create([
            'body' => request('body'),
            'post_id' => $post->id
            ]);

        return back();
   }
}

这是我的观点:

@extends('layouts.master')
@section('content')
<div class="col-sm-8 blog-main">
    <h1>{{ $post->title }}</h1>
    {{ $post->body }}
    <hr>
    <h5>Comments</h5>
    <div class="comments">
        <ul class="list-group">
            @foreach ($post->comments as $comment)
            <li class="list-group-item">
            <strong>
                {{ $comment->created_at->diffForHumans() }}: &nbsp;
            </strong>
            {{ $comment->body }}
            </li>
            @endforeach
        </ul>
    </div>
    <hr>
    <!-- Add Comment -->
    <div class="card">
        <div class="card-block">
            <form method="POST" action="/blog/public/posts/{{ $post->id }}/comments" >
                <div class="form-group">
                    <textarea name="body" placeholder="Your Comment" class="form-control"></textarea>
                </div>
                 <div class="form-group">
                     <button type="submit" class="btn btn-primary">Add Comment</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

变量 $post$comment 成功传递给视图,因为检索内容和评论工作正常,但是当我尝试提交新评论时,我得到了 MethodNotAllowedHttpException

【问题讨论】:

  • 您能否发布您的路线文件中处理此表单的部分?

标签: php laravel laravel-5.2


【解决方案1】:

你的表单动作应该是这样的

/posts/{{ $post->id }}/comments

【讨论】:

    【解决方案2】:
    1. 为您的路线命名

      Route::post('/posts/{post}/comments', 'CommentsController@store')->name('comments.create');
      
    2. 添加 csrf 令牌并使用辅助方法 route() 访问表单中的路由

      <form method="POST" action="{{ route('comments.create', ['post' => $post->id]) }}" >
          {{ csrf_field() }}
          <div class="form-group">
              <textarea name="body" placeholder="Your Comment" class="form-control"></textarea>
          </div>
           <div class="form-group">
               <button type="submit" class="btn btn-primary">Add Comment</button>
          </div>
      </form>
      
    3. 更改你的函数签名

      public function store($post)
      {
          Comment::create([
              'body' => request('body'),
              'post_id' => $post
          ]);
      
          return back();
      }
      

    【讨论】:

    • 我这样做了,但还是同样的问题。
    • composer dump-autoload然后再试一次
    • @hesham 尝试更改您的函数签名
    • 还是同样的问题,我把我的项目推到了github,方便查看代码。希望你能检查一下:github.com/HeshamAbusaif/Blog-Laravel
    • 尝试将Route::post('/posts/{post}/comments放在Route::get('/posts/{post}'之前
    猜你喜欢
    • 2016-08-31
    • 1970-01-01
    • 2016-06-23
    • 2016-06-04
    • 2016-06-15
    • 2016-06-20
    • 2016-12-26
    • 2016-09-18
    • 2016-07-30
    相关资源
    最近更新 更多