[英]Laravel 4.2 continue routing
Imagine there is the following url structure: 假设有以下网址结构:
someurl.axyz/{post-slug}
someurl.axyz/{page-slug}
post-slug
and page-slug
are stored in the unique database table column. post-slug
和page-slug
存储在唯一数据库表列中。 What is the "most" elegant way to handle the routes with Laravel 4.2 (and if there is a difference the way with Laravel v5.2)? Laravel 4.2处理路由的“最”优雅方法是什么(如果Laravel v5.2的方法有所不同)?
I resolved this using binding parameter to a model. 我使用绑定参数解决了这个问题。 The following code is at the bottom of routes.php file.
以下代码位于routes.php文件的底部。 I would like to know whether there is simpler solution for this task.
我想知道此任务是否有更简单的解决方案。
Route::bind('directslug', function($value, $route) {
$post = Post::where('slug', $value)->first();
if ($post) {
return $post;
}
$page = Page::where('slug', $value)->first();
if ($page) {
return $page;
}
throw new NotFoundHttpException;
});
Route::get('{directslug}', function(Post $post) {
return $post;
});
Route::get('{directslug}', function(Page $page) {
return $page;
});
App::missing(function($exception) {
return Response::view('error.missing', array('title' => '404 Page not Found'), 404);
});
I handle everything with route model binding , fairly similarly to your solution. 我使用路由模型绑定来处理所有事情,与您的解决方案非常相似。
In my opinion you should keep a separate model for each route, otherwise you might end up with a post having the same slug of a page... even if I'm sure you've taken care of this. 在我看来,您应该为每个路线保留一个单独的模型,否则,您最终可能会获得一整页都相同的帖子……即使我确定您已经做好了这件事。 You use two different routes afterall, why should you always query for a post if someone is looking for a page?
毕竟,您使用两条不同的路线,如果有人正在寻找页面,为什么还要始终查询帖子?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.