2

I trying to find a solution to pass a any number of parameter using route.

As an example, if my route is

$routes->get('pages/section/widgets/(:num)/(:num)/(:num)', 'Section::widgets/$1/$2/$3');

Here I have to promptly declare number of parameters I am passing i.e. 3.

But if I am not sure about number of parameters, then how can I pass that in ci4? I went through documentation and tried all the possible placeholders. But it won't work as expected.

For more clarity in Laravel we use -

Route::get('/{page?}', 'Frontend\HomeController@inside')->where('page', '.*');

So in laravel it accept all the parameter and redirect to it's respective method.

Thank you in advance.

Abhishek Honrao
  • 780
  • 5
  • 28

1 Answers1

5

After some research and going through documentation. I have come up with solution that it is not possible with routing. We can achieve this by in-built library URI class. Refer documentation for more information.

As an example - If I have url like http://example.com/test1/test2/test3. Then in routes we can declare like -

$routes->get('/(:any)', 'Home::index');

But in controller -

$uri = service('uri');
print_r($uri->getSegments());//this will give you all the segments in array.

So this will print -

Array ( [0] => test1 [1] => test2 [2] => test3 )

Thanks.

Abhishek Honrao
  • 780
  • 5
  • 28