I am looking at routing to a Controller for GET URL whose parameters can vary in number or the order in which they appear in the URL. There could be many such combinations and I want to invoke the same controller action for all of these URLs
Examples of how my URLs could look like:
- Route::get('route1/id/{id}', 'Controller1@controllerAction1');
- Route::get('route1/id/{id}/name/{name}', 'Controller1@controllerAction1');
- Route::get('route1/name/{name}', 'Controller1@controllerAction1');
- Route::get('route1/id/{id}/name/{name}/orderby/{orderby}', 'Controller1@controllerAction1');
- Route::get('route1/id/{id}/orderby/{orderby}', 'Controller1@controllerAction1');
Also in the Controller action, I ultimately want to break this query string into an array. For the second example mentioned above, I want the query string id/{id}/name/{name} to be converted to array ('id' => {id}, 'name' => {name})
To invoke the same controller action for all different variations of the URLs, I have the following code in my routes.php:
Route::get('route1{all}', 'Controller1@controllerAction1')->where('all', '.*')
which seems to invoke the "controllerAction1" of Controller1 for the different types of URLs mentioned above.
And in the function controllerAction1, I am doing
$route_input = Route::input('all');
var_dump($route_input);
which prints "/id/1/name/xyz" when I hit http://example.com/laravel/public/route1/id/1/name/xyz
I would like to know if:
- Doing Route::get('route1{all}', 'Controller1@controllerAction1')->where('all', '.*') is the right method to invoke same action for variable combination of get parameters?
- Does Laravel offer any function to convert "/id/1/name/xyz" to array('id' => 1, 'name' => 'xyz') or I need to write custom function?
- Is there a better way to achieve my requirements?