Riyas
Riyas

Reputation: 68

Laravel routing pattern - url having multiple items

My URL is something like this: http://localhost/sample/public/category/Electronics/Computers and Tablets/Tablets/iPad

Here is the list of Electronics, Computers and Tablets, Tablets etc are Categories and Sub categories. There may be N number of categories...

Is it possible to write a patterns that match the N number of categories instead of mentioning them individually as {cat1?}/{cat2?}/{cat3?} like it is done in the code below?

Route::group(['prefix' => 'category'], function () {
        Route::get('/','CategoryController@show');
        Route::get('/{cat1?}/{cat2?}/{cat3?}', 'CategoryController@show');        
    }); 

Any help is appreciated.

Upvotes: 0

Views: 903

Answers (2)

featherbits
featherbits

Reputation: 862

You can make Route with one parameter as regex pattern. Something like category/{params} where {params} is something like (/.+)+. Then in your controllers action you can get list of categories by parsing this parameter. $categoriesArray = split('/', trim($params, '/'));

Upvotes: 3

Gaurav Dave
Gaurav Dave

Reputation: 7474

Try this:

Route::get('/{cat1}/{cat2}/{cat3}', 'CategoryController@show')->where(['cat1' => '^(Electronics|Computers)', 'cat2' => '^(iPad)']);

See, if that help.

Upvotes: 0

Related Questions