Ramesh Thapa

Sharing my personal opinion and notes

laravel url parameter routing guideline with controller

laravel url parameter routing guideline with controller

When we develop website using laravel framework routing is essential part of developement both for frontend and backend. After having simple understanding how laravel we use simple routing with parameter as our requirement. As Laravel is MVC ( Model View Controller ) framework, url parameters is also related to both controller and single view of website.

There is generally two types of url we can see in website to use single view.  One has paramter attached to url with simple value ( without question mark) and another has parameter attached to the url after question mark (?).

  1. If you want simple url like :

https://www.mysite.com/controllerMethod/value

Your route will look something like this

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

You can get the value of this parameter in our controller function by passing the same name of variable in your controller method as we used in our route.

public function controllerMethod($key) {
//echo $key;

return view(‘…….’, compact(‘….’));
}

2. If you want parameter attached to the url after the question mark like

https://www.mysite.com/controllerMethod?key=value

We generally need this kind of url when we need to  search value of url and display value in view in single page.

For this your route  will be look like

Route::get(‘controllerMethod’, $controller . ‘controllerMethod’);

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

public function controllerMethod() {
$key = Input::get(‘key’);
//echo $key;

return view(‘…..’, compact(‘….’));
}

Leave comment

Your email address will not be published. Required fields are marked with *.