W
W
WhatYouDoing2022-02-09 16:33:36
Laravel
WhatYouDoing, 2022-02-09 16:33:36

How to distinguish between category and product routes?

The bottom line is that I do the routes of the online store according to the CNC principle, I can’t understand how to distinguish the routes of categories and products
, we have the following:

Route::prefix('catalog')->group(function () {
    Route::get('/{category:slug}', [CatalogController::class, 'categoryDetail'])->name('category');
    Route::get('/{category:slug}/{subcategory:slug}', [CatalogController::class, 'categorySubDetail'])->name('subcategory');
});

That is, urls can be of the following types http://.../catalog/categoria1, http://.../catalog/categoria1/podkategoriathis is implemented, but it starts to be interesting how to implement the product route when the url can be http://.../catalog/categoria1/product1either
http://.../catalog/categoria1/podkategoria/product2


I can’t understand for a day now, I don’t think I’ll think of it, thanks to everyone, any advice will be very glad!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2022-02-09
@WhatYouDoing

At the level of routing in this form - no way to distinguish. The method CatalogController::categorySubDetailmust first look for a product with that slug, and if it doesn't find it, then look for the directory. Well, or vice versa, as you yourself determine the priority.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question