【发布时间】:2021-09-04 00:52:26
【问题描述】:
我正在尝试创建自定义路线。必须采用这种格式:http://localhost:8000/home-back-to-school,但我得到一个 404 not found 错误。 http://localhost:8000/posts/home-back-to-school 有效,但这不是我想要的。
我在 web.php 上的路由定义为:Route::resource('posts',PostsController::class);
我通过添加以下代码修改了Route Service Provider:
parent::boot();
Route::bind('post',function($slug){
return Post::published()->where('slug',$slug)->first();
});
发布的范围在 Post Model 文件(Post.php)中定义为:
public function scopePublished()
{
return $this->where('published_at','<=',today())->orderBy('published_at', 'desc');
}
我以前用过 laravel 5.x,现在用 laravel 8.x 文档链接:Laravel 8 Documentation
【问题讨论】:
-
所以定义一个匹配
home-back-to-school模式的路由。