Я создал динамические маршруты категорий, добавив в свое приложение пользовательский класс ( как я это сделал здесь ), теперь мне нужно, чтобы мой блейд работал с этим динамическим путем.
Logic
на основе категорий глубины, которые создаст мой 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 пулю, в то время как моя категория может пройти 2, 3 или 4 пули, и для меня не имеет смысла проложить несколько маршрутов и продолжать повторять Route::get('/category/{slug}/{slug}/{slug}
вот так.
Как я уже сказал, я не уверен в этом, пожалуйста, поделитесь своей идеей и решениями, если можете.
UPDATE
на основе Leena Patel
ответа Я изменил свой маршрут, но когда я получаю более 1 пули в моем URL, возвращается ошибка:
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'));
}
есть идеи?