RouteCollection.php第218行中的MethodNotAllowedHttpException我该怎么办?

时间:2016-12-14 11:20:21

标签: php mysql laravel laravel-5.3

我目前正在研究论坛系统(laravel 5.3),我收到错误,可能是因为我的路线设置中出现了错误。

    <?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/

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

Auth::routes();

Route::get('/index', 'IndexController@index')->name('index');
Route::get('/thread/{id}', 'ThreadController@showThread')->name('thread_detail');
Route::get('/privacybeleid', 'PagesController@privacyPolicy');
Route::get('/over-ons', 'PagesController@about');

Route::group(['middleware' => 'auth'], function() {

Route::get('/thread/nieuw', 'ThreadController@showForm')->name('thread_form');
Route::post('/thread', 'ThreadController@create')->name('create_thread');
Route::post('/thread/{id}/reply', 'CommentController@create')->name('create_comment');
});

当我想看一个帖子(website.com/thread/{id})时,我没有得到任何错误,一切正常。但是当我想在主题(website.com/thread/{id}/reply)上添加新评论或创建新主题(website.com/thread/nieuw)时,我收到以下错误:

  

RouteCollection.php第218行中的MethodNotAllowedHttpException:

我尝试将路由::获取更改为路由::发布(反之亦然),但后来又出现了其他错误。

当我访问website.com/thread/nieuw时,我什么都没看到(页面不存在)。即使我有一个会话(登录)。

顺便说一句:有路由/ web.php的提示?让我知道

注意:我是荷兰人,所以&#34; nieuw&#34;意味着&#34;新&#34;

3 个答案:

答案 0 :(得分:0)

尝试这条路线:

Route::post('/thread/{id}/reply', 'CommentController@create')->name('create_comment');

在此之前:

Route::get('/thread/{id}', 'ThreadController@showThread')->name('thread_detail');

答案 1 :(得分:0)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;
use App\Http\Requests;
use App\Thread;
use App\Comment;

class CommentController extends Controller
{
    public function create($id, Request $req)
    {
        $user = Auth::user();
        $thread = Thread::findOrFail($id);
        $body = $req->get('body');
        $comment = new Comment([
            'body' => $body
        ]);
        $comment->user_id = $user->id;
        $thread->comments()->save($comment);
        return redirect()->route('thread_detail', ['id' => $thread->id]);
    }
}

我的评论控制器

答案 2 :(得分:0)

您可能需要检查编辑表单方法

{{ Form::open(['class' => 'form-horizontal', 'route' => ['create_comment', $thread->id], 'method' => 'PUT']) }}