Laravel路由参数不起作用

时间:2017-12-10 12:43:50

标签: php laravel

任何帮助,为什么这不起作用,我使用的是Laravel 5.5.23版本,这是我的路线:

<?php



Route::get('/', function () {
    return view('welcome');
});

Route::resource('threads','ThreadController');
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('threads','ThreadController@index');
Route::get('threads/{channel}','ThreadController@index');
Route::get('threads/create','ThreadController@create');
Route::get('threads/{channel}/{thread}','ThreadController@show');
Route::post('threads','ThreadController@store');
Route::post('/threads/{channel}/{thread}/replies','ReplyController@store');
Route::get('/logout' , 'Auth\LoginController@logout');

这是ThreadController,实际上只是相关的方法:

public function __construct()
    {
        $this->middleware('auth')->except(['index','show']);
    }

    public function index($channel = null)
    {
        if($channel){
            //do something
        }
        else{
            $threads=Thread::latest()->get();
        }
        return view('threads.index',compact('threads'));
    }

问题是当我尝试访问/ threads / someChannel时它返回未找到,所以这是有问题的路线:Route::get('threads/{channel}','ThreadController@index');,所有其他路线都在工作,任何想法为什么这个不起作用?

1 个答案:

答案 0 :(得分:1)

Route::resource('threads','ThreadController')调用定义了7条前缀为threads的路由。你自己定义的一些路线被这个掩盖了。检查php artisan route:list以查看Route::resource呼叫首先为您注册的路由。在匹配路线方面,先到先得。

/threads/someChannel将匹配resource调用定义的路由:

GET        /threads/{thread}        ThreadController@show

由于您没有<{1}} 相关的定义,我会假设您发生了隐式模型绑定。它试图将该模型绑定到该参数,并且它无法通过该ID在数据库中找到它并因此而导致404。