2016-12-14 175 views
2

我目前正在研究一个论坛系统(laravel 5.3),并且我得到一个错误,可能是因为我在路由设置中设置了错误。RouteCollection.php中的MethodNotAllowedHttpException第218行我该怎么办?

<?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('/', '[email protected]')->name('index'); 

Auth::routes(); 

Route::get('/index', '[email protected]')->name('index'); 
Route::get('/thread/{id}', '[email protected]')->name('thread_detail'); 
Route::get('/privacybeleid', '[email protected]'); 
Route::get('/over-ons', '[email protected]'); 

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

Route::get('/thread/nieuw', '[email protected]')->name('thread_form'); 
Route::post('/thread', '[email protected]')->name('create_thread'); 
Route::post('/thread/{id}/reply', '[email protected]')->name('create_comment'); 
}); 

当我想看到一个线程(website.com/thread/{id}),然后我没有得到任何错误,一切正常,因为它应该。但是,当我想在主题上添加新评论(website.com/thread/{id}/reply)或创建新主题(website.com/thread/nieuw)时,出现以下错误:

MethodNotAllowedHttpException in RouteCollection.php line 218:

我试图改变路线::得到路线::后(反之亦然),但后来我得到另一个错误。

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

顺便说一下:有关于routes/web.php的提示吗?让我知道

注:我是荷兰人如此 “的Nieuw” 是指 “新”

+0

尝试把你的默认路由'''路线::得到( '/', '索引控制器@指数') - >名称( '指数');' “在底部,而不是在任何其他路线之前。 – Dev

+0

不幸的是,我仍然收到相同的错误信息。但我会正确调整它。 – Sygun

+0

为什么不使用Route :: post('/ thread/reply/{id}',..)来代替你的? –

回答

0

尝试把这个路线:

Route::post('/thread/{id}/reply', '[email protected]')->name('create_comment'); 

这个之前:

Route::get('/thread/{id}', '[email protected]')->name('thread_detail'); 
+0

如果这不起作用,请显示您要提交给'create_comment'路线的表单。 –

+0

不幸的是,我仍然得到相同的错误信息。 – Sygun

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]); 
    } 
} 

我的评论控制器

+0

请显示表格,而不是方法。 –

+0

请保留您的代码在您的问题编辑。不作为答案 – scottevans93

+0

我会在下次注意 Alexey Mezenin:你的意思是在我的意见? – Sygun

0

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

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