例如
-cat1
--cat2
---cat3
现在,为了解决类别,我使用以下命令创建了一个 URL 链接:
$categories = \Category::ancestorsAndSelf($last_category->id);
$uri = $categories->pluck('slug')->implode('/');
echo url($uri);
所以我有这样的事情:
http://examole.com/cat1/cat2/cat3
此外,为了解决一篇文章,我使用以下命令创建了一个 URL 链接:
$categories = \Category::ancestorsAndSelf($article->category_id);
$uri = $categories->pluck('slug')->implode('/') . '/' . $article->slug;
echo url($uri);
所以我有这样的事情:
http://examole.com/cat1/cat2/cat3/slug_article
如何通过 web.php 访问控制器中的这些功能?
在文章控制器
public function show(Article $article){
//
}
在类别控制器中
public function show(Category $category){
//
}
我尝试了这些步骤:
在web.php中
Route::get('{subCategories?}', 'postController@show')->where('subCategories', '.+');
Route::get('{subArticles?}', 'articleController@show')->where('subArticles', '.+');
在RouteServiceProvider.php启动函数
Route::bind('subCategories' , function ($route)
{
//test:http://examole.com/cat1/cat2/cat3
// echo $route: cat1/cat2/cat3
//what should I do to check the address is correct?
if(true) return $category;//it return to show(Category $category)
});
Route::bind('subArticles' , function ($route)
{
//test:http://examole.com/cat1/cat2/cat3//slug_article
// echo $route: null
//what should I do to check the address is correct?
if(true) return $article;//I want to return to show(Article $aericle) function.
});
这种方法正确吗?如何将嵌套类别链接与文章链接分开?(并检查两者?)