1

I have the following routes for a webshop with categories and products:

Route::name('shop.products.view')->get('/p/{productUrl}', 'Shop\ProductsController@view');
Route::name('shop.categories.view')->get('/c/{categoryOne}/{categoryTwo?}/{categoryThree?}', 'Shop\CategoriesController@view')

categoryTwo is a subcategory of categoryOne

categoryThree is a subcategory of categoryTwo

This works perfect but I need to put .html at the end so the url's are exactly the same as the url's from the old webshop.

For the productpage this is no problem:

Route::name('shop.products.view')->get('/p/{productUrl}.html', 'Shop\ProductsController@view');

If I do this for the category page it doesn't work when the optional parameters are not filled.

Route::name('shop.categories.view')->get('/c/{categoryOne}/{categoryTwo?}/{categoryThree?}.html', 'Shop\CategoriesController@view')

This results in: domain.com/c/category1//.html

Any ideas on how to solve this so I get:

domain.com/c/category1.html

domain.com/c/category1/category2.html

domain.com/c/category1/category2/category3.html

  • Hi, i think you may try to separate your route logic with : https://laravel.com/docs/5.8/routing#route-groups – loic.lopez Apr 03 '19 at 13:55
  • Hmm no I don't think so. How will this solve the extension problem? – Maykel Boes Apr 03 '19 at 14:34
  • This will definitely help anyone still interested in a solution to the question above : https://stackoverflow.com/questions/22287649/add-url-extensions-to-laravel-routes – cd4success Aug 02 '23 at 01:31

1 Answers1

0

You have two options:

  1. Use category2 and category3 as query parameters, after .html, and pass them as ?category2=aaa&category3=bbb;
  2. Define multiple routes under the same group as follows (see next code example). I don't like this solution but it should work if you call the routes correctly and not from the url builder URL::action('Shop\CategoriesController@view').
    Route::name('shop.products.view.')->group(function () {
        Route::get('/c/{categoryOne}/{categoryTwo}/{categoryThree}.html', 'Shop\CategoriesController@view');
        Route::get('/c/{categoryOne}/{categoryTwo}.html', 'Shop\CategoriesController@view');
        Route::get('/c/{categoryOne}.html', 'Shop\CategoriesController@view')
    });
clod986
  • 2,527
  • 6
  • 28
  • 52
  • 1. If I use them as query parameters the url is not exactly the same. It has to be this pattern. 2. I tried your solution but it only works for the first category, the other two are added as query parameters after .html – Maykel Boes Apr 03 '19 at 14:41