RouteCollection.php 第 218 行中的 MethodNotAllowedHttpException 我该怎么办?
MethodNotAllowedHttpException in RouteCollection.php line 218 what can I do?
我目前正在使用论坛系统 (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) 时,出现以下错误:
MethodNotAllowedHttpException in RouteCollection.php line 218:
我尝试将 Route::get 更改为 Route::post (反之亦然),但后来我又遇到了另一个错误。
当我访问 website.com/thread/nieuw 时,我什么也看不到(页面不存在)。即使我有会话(已登录)。
顺便提一下:routes/web.php有提示吗?让我知道
注:我是荷兰人所以"nieuw"表示"new"
尝试放这条路线:
Route::post('/thread/{id}/reply', 'CommentController@create')->name('create_comment');
在此之前:
Route::get('/thread/{id}', 'ThreadController@showThread')->name('thread_detail');
<?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]);
}
}
我的评论控
您可能需要检查编辑表单方法
{{ Form::open(['class' => 'form-horizontal', 'route' => ['create_comment', $thread->id], 'method' => 'PUT']) }}
我目前正在使用论坛系统 (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) 时,出现以下错误:
MethodNotAllowedHttpException in RouteCollection.php line 218:
我尝试将 Route::get 更改为 Route::post (反之亦然),但后来我又遇到了另一个错误。
当我访问 website.com/thread/nieuw 时,我什么也看不到(页面不存在)。即使我有会话(已登录)。
顺便提一下:routes/web.php有提示吗?让我知道
注:我是荷兰人所以"nieuw"表示"new"
尝试放这条路线:
Route::post('/thread/{id}/reply', 'CommentController@create')->name('create_comment');
在此之前:
Route::get('/thread/{id}', 'ThreadController@showThread')->name('thread_detail');
<?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]);
}
}
我的评论控
您可能需要检查编辑表单方法
{{ Form::open(['class' => 'form-horizontal', 'route' => ['create_comment', $thread->id], 'method' => 'PUT']) }}