How to Get Route Parameters in Controller of Laravel 5.2
Requests in Laravel can contain data in two different ways i.e. as URL route parameters or as request parameters. This article explains how to get URL route parameters inside controller.
The Route Parameters are those which are submitted as part of the URL Pattern. Assume users access the url /users/search/lahore. Here we can treat "lahore" as route parameter for city. Inside Controller we must get city name in a variable so that we can query database for users living in Lahore. To get route parameters, we define appropriate URL pattern in routes.php file. Lets see the mapping for above URL in routes.php file and how we would receive the variable inside controller.
We can receive the value of city placeholder inside controller by declaring just an argument in search method, as below:
Below is the output screen:
and the controller that retrieves the variables would be like below:
To keep the post focused, I have skipped making separate view and communicating with database. If we can retrieve route parameters, we can use them as we want. Below is the sample output for the above define URL Pattern:
So, only city is required. Other parameters are optional. Below is the updated controller code with search method.
The only important point is, you must declare default value for the method argument that is declared as optional. If you skip an route parameters, optional value would be used inside method. If you pass a route parameter in URL pattern, the argument value would be updated as per parameter value. Below I have given some sample output screen to demonstrate when the optional value is used and when the parameter value is used.
I hope you have well understood all major concepts about using Route Parameters in Laravel. Please help me by sharing this article on social media and forums if you like it.
The Route Parameters are those which are submitted as part of the URL Pattern. Assume users access the url /users/search/lahore. Here we can treat "lahore" as route parameter for city. Inside Controller we must get city name in a variable so that we can query database for users living in Lahore. To get route parameters, we define appropriate URL pattern in routes.php file. Lets see the mapping for above URL in routes.php file and how we would receive the variable inside controller.
Route::get('/users/search/{city}', 'UsersController@search');
We can receive the value of city placeholder inside controller by declaring just an argument in search method, as below:
namespace App\Http\Controllers; class UsersController extends Controller { public function search($city){ return $city; } }
Below is the output screen:
How to Get Multiple Route Parameters in Laravel Controller
In same way, you can receive more than URL Path variables too. For example, to receive two parameters we can define another mapping in routes.php like this:Route::get('/users/search/{city}/{interests}', 'UsersController@searchByCityAndInterests');
and the controller that retrieves the variables would be like below:
namespace App\Http\Controllers; class UsersController extends Controller { public function searchByCityAndInterests($city, $interests){ // query data to fetch to users per parameters sent to function return "City = " . $city . ". Interests = " . $interests; } }
To keep the post focused, I have skipped making separate view and communicating with database. If we can retrieve route parameters, we can use them as we want. Below is the sample output for the above define URL Pattern:
How to Use Optional Route Parameters
In above two examples, we used two separate methods only because the number of parameters were different. Its not clean solution to make separate methods only because the number of parameters are different. We should try to make one method to handle one type of request e.g. search. But the issue is, how we can map parameters to controller method arguments. I have update the search method and routing rule defined above to handle multiple parameters using single method (optional parameters are indicated with ? symbol).Route::get('/users/search/{city}/{interests?}/{gender?}/{age?}', 'UsersController@search');
So, only city is required. Other parameters are optional. Below is the updated controller code with search method.
namespace App\Http\Controllers; class UsersController extends Controller { public function search($city, $interests = "nothing", $gender = "Male", $age = null) { return "City : " . $city . ", Interests = " . $interests . ", Gender = ". $gender . ", Age = " . $age; } }
The only important point is, you must declare default value for the method argument that is declared as optional. If you skip an route parameters, optional value would be used inside method. If you pass a route parameter in URL pattern, the argument value would be updated as per parameter value. Below I have given some sample output screen to demonstrate when the optional value is used and when the parameter value is used.
I hope you have well understood all major concepts about using Route Parameters in Laravel. Please help me by sharing this article on social media and forums if you like it.
I have been following you for a couple of months now but this is my first time commenting on a blog post. Thank you for sharing your knowledge and experience with us. Keep up the good work. Already bookmarked for future reference.
ReplyDeleteSAP training in Chennai
Can you tell me how to pass multiple parameter from click to button or links?
DeleteYou are right but, you are missed one thing that is "how to pass multiple parameter from view file where we will click on any particulate button or links?". So can you mansion this thing in it??
ReplyDeleteThanks for your sharing, However is there a way to pass an optional variable without passing a previous optional NULL one?
ReplyDeleteLike in your example :
$city, $interests = null, $gender = "Male", $age = null
How can I pass $interests -> null and age -> 18 in this case?