7

Is there any way to declare unlimited number of parameters in routes of Laravel 5 similar to Codeigniter?

I am going to build a large application and declaring each and every parameter in route file for each function is not possible. I tried searching a lot but didn't got any solution.

Nishant Kango
  • 146
  • 1
  • 9
  • 1
    Take a look at the second part of [this answer](http://stackoverflow.com/a/27343858/1903366) – lukasgeiter Jul 14 '15 at 13:25
  • So it means i need to declare route for each and every controller. Is there any option something similar to Codeigniter? – Nishant Kango Jul 14 '15 at 13:30
  • Have you read the **second part** of the answer? It may be not the same as in codeigniter (which I don't know much about anyways) but you can have a route that takes one route parameter that contains slashes. You then have to split that one parameter and you'll get an array of all segments... – lukasgeiter Jul 14 '15 at 13:36
  • @lukasgeiter It did the trick but not completely. I have to use it for all controllers. That is the said part. – Nishant Kango Jul 15 '15 at 06:13

1 Answers1

14

You can use this

//routes.php
Route::get('{id}/{params?}', 'YourController@action')->where('params', '(.*)');

Remember to put the above on the very end (bottom) of routes.php file as it is like a 'catch all' route, so you have to have all the 'more specific' routes defined first.

//controller 
class YourController extends BaseController {

    public function action($id, $params = null)
    {
        if($params) 
        {
            $params = explode('/', $params);
            //do stuff 
        }
    }
}
Lakhwinder Singh
  • 5,536
  • 5
  • 27
  • 52
  • Will this generate SEO friend URL's? I want to have urls http://www.example.com/controller/function/param1/param2/param3/... ? – Nishant Kango Jul 14 '15 at 13:34
  • I tried your solution. It works but i have to use explode in each and every function of controller for all controllers which is not a good solution for large applications. – Nishant Kango Jul 15 '15 at 06:16
  • @NishantKango I agree with your comments, So you can place this in your BaseController __constructor method or create any middleware file and include in your BaseController __constructor method. – Lakhwinder Singh Jul 15 '15 at 08:48
  • 1
    @LuckySaini Its helpfull for me. Thanks – Gurpreet Singh Apr 18 '18 at 06:27
  • 1
    @Lakhwinder Singh I had no idea this was possible - what a great option. Another note, this works well in groups too - which is a nice catchall limited to a single prefix – ChronoFish Feb 14 '20 at 16:36