How to get value of URL parameters into Controller (Laravel)

How to get value of URL parameters into Controller (Laravel)

 

How to get the value of URL parameters into Controller (Laravel)


1.  If you have your parameter attached to the URL after the question mark like

http://www.yoursite.com/controllerMethod?key=value

Your route for this controller will look something like this

Route::get('controllerMethod', $controller . 'controllerMethod');

You can get the value of this parameter in your controller function by

public function controllerMethod(Request $request) {
   $key = $request->key
   echo $key;
}

2. If you have your parameter attached to the URL without a question mark like

http://www.yoursite.com/controllerMethod/value

Your route for this controller will look something like this

Route::get('controllerMethod/{key}', $controller . 'controllerMethod');

You can get the value of this parameter in your controller function bypassing the same name of the variable in your controller method as you used in the route.

public function controllerMethod($key) {
   echo $key;
}
Reactions

Post a Comment

0 Comments

close