Laravel 5:组中的路由模型绑定

Laravel 5:组中的路由模型绑定,laravel,binding,model,routes,Laravel,Binding,Model,Routes,我在RouteServiceProvider中绑定了一个路由模型: public function boot(Router $router) { parent::boot($router); $router->model('article', 'App\Article'); } 我的路线组: Route::group(['prefix' => 'articles'], function(){ //some routes ....

我在
RouteServiceProvider
中绑定了一个路由模型:

public function boot(Router $router)
    {
        parent::boot($router);

        $router->model('article', 'App\Article');
    }
我的路线组:

Route::group(['prefix' => 'articles'], function(){
  //some routes ....

  Route::group(['prefix' => '{article}'], function(){
    Route::get('', [
      'as' => 'article.show',
      'uses' => 'ArticlesController@show'
    ]);

    Route::get('comments', [
       'as' => 'article.comments',
       'uses' => 'ArticlesController@comments'
    ]);
  });
});
/articles/666
非常有效


/articles/666/comments
向我显示Http未找到异常。

我能够重新创建此问题,但仅当数据库中没有id为666的文章时

奇怪的是,当我没有路由绑定设置时,我没有遇到这个问题


尝试创建一篇id为666的文章,或者将id更改为您确实拥有的文章,应该可以正常工作。如果没有,您可能会有另一条路由覆盖此路由。运行命令
php artisan route:list
获取所有路由的列表。如果要缓存路由,请确保同时重新生成缓存。

使用路由,可以将模型直接注入操作:

// ArticlesController.php
...
public function show(Article $article) {
    return response()->json($article);
}
但是请记住,您需要使用“bindings”中间件组来确保模型隐式地从路由获取。 例如,在你的路线配置中:

Route::middleware('bindings')->group(function() {
    Route::group(['prefix' => 'articles'], function(){
        Route::group(['prefix' => '{article}'], function(){
            Route::get('', [
                'as' => 'article.show',
                'uses' => 'ArticlesController@show'
            ]);

            Route::get('comments', [
                'as' => 'article.comments',
                'uses' => 'ArticlesController@comments'
            ]);
        });
    });
});

这似乎不是一件有很好记录的事情。

我真的不明白为什么?OP在组前缀中已经有了文章绑定,并且说
/articles/666
工作得很好。666只是一个例子,它可能是另一个id,仍然不工作。奇怪的是,您显示的代码对我来说工作得很好(只要我在数据库中有一篇id为的文章)。问题一定在别的地方。