【发布时间】:2019-04-06 07:59:52
【问题描述】:
我已经通过向我的应用程序 (how i did it is here) 添加自定义类来制作动态类别路由,现在我需要让我的刀片使用这个动态路径。
逻辑
基于我的 url 将创建的深度类别,例如:
site.com/category/parent
site.com/category/parent/child
site.com/category/parent/child/child
etc.
到目前为止,我的视图只是为 site.com/category/parent 加载其他 URL,它返回 404 错误。
代码
CategoryRouteService
class CategoryRouteService
{
private $routes = [];
public function __construct()
{
$this->determineCategoriesRoutes();
}
public function getRoute(Category $category)
{
return $this->routes[$category->id];
}
private function determineCategoriesRoutes()
{
$categories = Category::all()->keyBy('id');
foreach ($categories as $id => $category) {
$slugs = $this->determineCategorySlugs($category, $categories);
if (count($slugs) === 1) {
$this->routes[$id] = url('category/' . $slugs[0]);
}
else {
$this->routes[$id] = url('category/' . implode('/', $slugs));
}
}
}
private function determineCategorySlugs(Category $category, Collection $categories, array $slugs = [])
{
array_unshift($slugs, $category->slug);
if (!is_null($category->parent_id)) {
$slugs = $this->determineCategorySlugs($categories[$category->parent_id], $categories, $slugs);
}
return $slugs;
}
}
CategoryServiceProvider
class CategoryServiceProvider
{
public function register()
{
$this->app->singleton(CategoryRouteService::class, function ($app) {
// At this point the categories routes will be determined.
// It happens only one time even if you call the service multiple times through the container.
return new CategoryRouteService();
});
}
}
model
//get dynamic slug routes
public function getRouteAttribute()
{
$categoryRouteService = app(CategoryRouteService::class);
return $categoryRouteService->getRoute($this);
}
blade
//{{$categoryt->route}} returning routes
<a class="post-cat" href="{{$category->route}}">{{$category->title}}</a>
route
//show parent categories with posts
Route::get('/category/{slug}', 'Front\CategoryController@parent')->name('categoryparent');
controller
public function parent($slug){
$category = Category::where('slug', $slug)->with('children')->first();
$category->addView();
$posts = $category->posts()->where('publish', '=', 1)->paginate(8);
return view('front.categories.single', compact('category','posts'));
}
注意:我不确定这一点,但我认为我的路线有点静态!我的意思是它只会得到 1 个 slug,而我的类别可以深入 2、3 或 4 个 slug,对我来说,走多条路线并像这样不断重复 Route::get('/category/{slug}/{slug}/{slug} 是没有意义的。
正如我所说,我对此不确定,如果可以,请分享您的想法和解决方案。
更新
基于Leena Patel 的回答,我更改了路线,但是当我的网址中出现超过 1 个 slug 时,它返回错误:
Example
route: site.com/category/resources (works)
route: site.com/category/resources/books/ (ERROR)
route: site.com/category/resources/books/mahayana/sutra (ERROR)
error
Call to a member function addView() on null
开
$category->addView();
当我评论它返回 $posts 部分的错误时。然后我的刀片出错,我返回类别标题{{$category->title}}
所以基本上它似乎无法识别返回类别路线视图的此功能。
here is my function
public function parent($slug){
$category = Category::where('slug', $slug)->with('children')->first();
$category->addView();
$posts = $category->posts()->where('publish', '=', 1)->paginate(8);
return view('front.categories.single', compact('category','posts'));
}
有什么想法吗?
【问题讨论】: