Laravel Routing Route Parameters


Example

You can use route parameters to get the part of the URI segment. You can define a optional or required route parameter/s while creating a route. Optional parameters have a ? appended at the end of the parameter name. This name is enclosed in a curly braces {}

Optional Parameter

Route::get('profile/{id?}', ['as' => 'viewProfile', 'uses' => 'ProfileController@view']);

This route can be accessed by domain.com/profile/23 where 23 is the id parameter. In this example the id is passed as a parameter in view method of ProfileController. Since this is a optional parameter accessing domain.com/profile works just fine.

Required Parameter

Route::get('profile/{id}', ['as' => 'viewProfile', 'uses' => 'ProfileController@view']);

Note that required parameter's name doesn't have a ? at the end of the parameter name.

Accessing the parameter in controller

In your controller, your view method takes a parameter with the same name as the one in the routes.php and can be used like a normal variable. Laravel takes care of injecting the value:

public function view($id){
    echo $id;
}