There is a URL to come to: site.com/catalog/cat1/cat2/25-rose-1.html , where cat1 cat2 are categories, 25-rose is the product alias, and 1 is its ID.

I do this:

 Route::get('catalog/{parentCategory}/{childrenCategory}/{productAlias}-{productId}.html', function($parentCategory, $childrenCategory, $productAlias, $productId){ echo 'Parent category: '. $parentCategory . ' Children category: '.$childrenCategory. ' Product alias: '.$productAlias. ' Product ID: '.$productId; }); 

But the result is:

 Parent category: cat1 Children category: cat2 Product alias: 25 Product ID: rose-1 

Question: how to make the product ID Laravel look at the last - in the link?

    1 answer 1

    You can specify in the route that the productAlias parameter uses the value of the type 25-rose using Regular Expression Constraints :

     Route::get('catalog/{parentCategory}/{childrenCategory}/{productAlias}-{productId}.html', function($parentCategory, $childrenCategory, $productAlias, $productId) { echo 'Parent category: '. $parentCategory . ' Children category: '.$childrenCategory. ' Product alias: '.$productAlias. ' Product ID: '.$productId; })->where('productAlias', '([0-9]+)-([A-Za-z]+)'); 

    In this case, the output will be as follows:

     Parent category: cat1 Children category: cat2 Product alias: 25-rose Product ID: 1 

    You can also clarify other values, for example, that the productId only a number, there are examples in the documentation how to do this.

    • Thank! I've tested another problem now: the link /catalog/flowers/roses/52-roses-60sm-5.html shows that 52-roses is an alias, and 60sm-5 is an ID. When specifying ->where('productId', '([0-9]+)') throws 404. - Alexxosipov
    • UPD: added ([-\w]+) to the regular productAlias, it ended up like this: ->where('productAlias', '([0-9]+)-([A-Za-z]+)-([-\w]+)'); . - Alexxosipov