0

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 > CategoryController
  • www.example.com/products/cat1/cat11/cat111/product-title > ProductController
  • www.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?

unor
  • 92,415
  • 26
  • 211
  • 360
Maverick
  • 21
  • 6

1 Answers1

1

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-title should be used carefully. It could lead to duplicate content issues when not adding the canonical tag properly.

We ended with the following setup:

  • www.site.com/shop/product-title > Route::get('/shop/{slug}', [\App\Http\Controllers\ProductController::class, 'show'])->name('product.show');
  • www.site.com/shop/categories/cat1/cat11/cat111 > Route::get('/shop/categories/{slugNum}', [\App\Http\Controllers\CategoryController::class, 'show'])->name('category.show')->where('slugNum', '(.*)');
  • www.site.com/contact-us > Route::get('/{any}', [\App\Http\Controllers\PageController::class, 'show'])->name('page.show')->where('any', '.*')->fallback();

Hope this helps.

Jonas
  • 42
  • 5