2016-11-21 36 views
2

我实施品类结构层次路线,部分产品将有一个一级类别,但其他的可能有两个或多个级别:Laravel:创建

/posts/cat2/post-sulg 

/posts/cat-1/sub-1/post-slug 

/posts/cat-3/sub../../../post-slug 

,因为我不知道它将如何深度和使用类别slu is仅适用于seo(我只能通过它的slu find发现帖子)创建处理此结构的路由的最佳方法是什么?

回答

3

你可以解决这个问题:

Route::get('posts/{categories}', '[email protected]') 
    ->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 
     (...) 
    } 

} 
+1

感谢,但你应该加上' - >在哪里( '类','^ [A-ZA-Z0- 9 -_ \ /] + $');'为了使它工作 – alex

+0

是的......你说得对,只需添加它... –