I need to create a site (e-shop) in Laravel using SEO-friendly and hierarchical URLs.
Some examples are:
www.example.com/cat1/cat11(which points to a category product-listing)www.example.com/cat1/cat11/cat111(which points to a category product-listing)www.example.com/cat1/cat11/cat111/product-title(which points to a product-details page)www.example.com/contact-us(which points to a page)
I can parse the above URLs and find the needed slug, but I am not sure which controller will be responsible for each route.
Possible thoughts:
1) Add prefix to each URL type, in order to know the controller e.g.
www.example.com/categories/cat1/cat11/cat111> CategoryControllerwww.example.com/products/cat1/cat11/cat111/product-title> ProductControllerwww.example.com/page/contact-us> PageController
2) Pre-generate and store in a table all the site's URLs. The table can contain two columns: the URL (cat1/cat11/cat111) and the model:id (category:8) A global controller will parse the URL and using the table can load the required model & id.
Any other idea?
We were already faced with this problem multiple times. I think there is no "best way". It depends on the given circumstances.
The structure
/products/cat1/cat11/cat111/product-titleshould be used carefully. It could lead to duplicate content issues when not adding the canonical tag properly.We ended with the following setup:
Route::get('/shop/{slug}', [\App\Http\Controllers\ProductController::class, 'show'])->name('product.show');Route::get('/shop/categories/{slugNum}', [\App\Http\Controllers\CategoryController::class, 'show'])->name('category.show')->where('slugNum', '(.*)');Route::get('/{any}', [\App\Http\Controllers\PageController::class, 'show'])->name('page.show')->where('any', '.*')->fallback();Hope this helps.