Laravel:为类别创建分层路径 [英] Laravel : create hierarchical route for category
本文介绍了Laravel:为类别创建分层路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在实现类别结构,一些产品可能有一个级别的类别,但另一些产品可能有两个或两个以上的级别:
/posts/cat2/post-sulg
/posts/cat-1/sub-1/post-slug
/posts/cat-3/sub../../../post-slug
由于我不知道它会有多深,而且使用类别插件仅适用于SEO(我只能通过其插件找到帖子),创建处理这种结构的最佳方法是什么?
推荐答案
可以通过以下方式解决:
Route::get('posts/{categories}', 'PostController@categories')
->where('categories','^[a-zA-Z0-9-_/]+$');
然后在控制器中
class PostController
{
public function categories($categories)
{
$categories = explode('/', $categories);
$postSlug = array_pop($categories)
// here you can manage the categories in $categories array and slug of the post in $postSlug
(...)
}
}
这篇关于Laravel:为类别创建分层路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文