Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare unlimited parameters in Laravel?

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.

like image 379
Nishant Kango Avatar asked Jul 14 '15 13:07

Nishant Kango


People also ask

What is Request$ Request in Laravel?

Laravel's Illuminate\Http\Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.

How to define routes in Laravel?

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider . The routes/web.php file defines routes that are for your web interface.

What is parameter in Laravel?

The required parameters are the parameters that we pass in the URL. Sometimes you want to capture some segments of the URI then this can be done by passing the parameters to the URL. For example, you want to capture the user id from the URL. Let's see the example without route parameters.


1 Answers

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 
        }
    }
}
like image 160
Lakhwinder Singh Avatar answered Sep 28 '22 07:09

Lakhwinder Singh