kaju74's avatar

How to setup unknown amount of route parameters?

Hi.

I've a problem understanding how to setup a route which can have a unknown amount of parameters. Let's say, we've items related to categeories. Each category can have parent categories:

Television -> Samsung -> LCD
Television -> Samsung -> Plasma
Television -> Philips -> LCD
Television -> Philips -> CRT
Hifi -> Amplifier -> Marantz -> Normal
Hifi -> Amplifier -> Marantz -> RackMounted -> FullSize
Hifi -> Amplifier -> Marantz -> RackMounted -> Halfsize
Hifi -> Amplifier -> Marantz -> Legacy

Each item can be attached to a category:

UE4032MH13 will be attached to Television -> Samsung -> LCD
NR1503 will be attached to Hifi -> Amplifier -> Marantz -> Normal

and so on...

So the url to render the item details could be:

http://xyz.com/category/television/samsung/lcd/ue4032mh13

The problem is, that I don't know to many sub-categories would exist in the future. Is there a common way to define a route excepting unlimited sub route parameters?

Thank you, kaju

0 likes
4 replies
kaju74's avatar

The best problem is to resolve it onself:

Route::get('/category/{hierarchy?}', 'CategoryController@index')->where('hierarchy', '.*');

CategoryController.php

public function index($hierarchy = null)
{
    if($hierarchy)
    {
        $hierarchy = explode('/', $hierarchy);
        return $hierarchy;
    }
    return 'no hierarchy passed!';
}

Hope this helps someone ;-) kaju

bashy's avatar
bashy
Best Answer
Level 65

You could use something like this in conjunction with the above.

// Catch all sub pages if they don't match the above routes
Route::get('{page}/{subs}', [
    'uses' => 'PageController@subPage',
])->where(['page' => '^((?!admin).)*$', 'subs' => '.*']);
1 like
bashy's avatar

@kaju74 Oh did you put that route in your post originally? I didn't see it, my reply was kinda the same! Sorry.

Please or to participate in this conversation.