Which Are The Route Parameters Used In Laravel Framework

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

In Laravel Routing is the ability to map URL requests to specific routes. It is simply a way of creating a request URL of your application. In Laravel, all requests are mapped with the help of routes. Basic routing routes the request to the associated controllers.

Which Are The Route Parameters Used In Laravel Framework

All the application routes are registered within the app/routes.php file. This file tells Laravel for the URIs it should respond to and the associated controller will give it a particular call. 
 

We have different Types of Route Parameters
  • Required Parameters

  • Optional Parameters

  • Regular Expression Constraints
     

1.Required Parameters

Sometimes you will need to capture segments of the URI within your route.

For example, you may need to capture a user's ID from the URL. You may do so by defining route parameters:

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});


Pass below url in google chrome to see above route output

http://localhost/blog/public/user/1


You may define as many route parameters as required by your route:

Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
//
});


Route parameters are always encased within {} braces and should consist of alphabetic characters, and may not contain a - character. Instead of using the - character, use an underscore (_). Route parameters are injected into route callbacks / controllers based on their order - the names of the callback / controller arguments do not matter.

2.Optional Parameters

Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:

Route::get('user/{name?}', function ($name = null) {
return $name;
});

Route::get('user/{name?}', function ($name = 'John') {
return $name;
});


3.Regular Expression Constraints

You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:

Route::get('user/{name}', function ($name) {
    //
})->where('name', '[A-Za-z]+');

Route::get('user/{id}', function ($id) {
    //
})->where('id', '[0-9]+');

Route::get('user/{id}/{name}', function ($id, $name) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);


Global Constraints

If you would like a route parameter to always be constrained by a given regular expression, you may use the pattern method. You should define these patterns in the boot method of your RouteServiceProvider:

/**
 * Define your route model bindings, pattern filters, etc.
 *
 * @return void
 */
public function boot()
{
    Route::pattern('id', '[0-9]+');

    parent::boot();
}


Once the pattern has been defined, it is automatically applied to all routes using that parameter name:

Route::get('user/{id}', function ($id) {
    // Only executed if {id} is numeric...
});


Encoded Forward Slashes

The Laravel routing component allows all characters except /. You must explicitly allow / to be part of your placeholder using a where condition regular expression:

Route::get('search/{search}', function ($search) {
    return $search;
})->where('search', '.*');
 

Example(1)

1.Create project using below code

composer create-project --prefer-dist laravel/laravel blog


2.Create Route in routes\web.php file as below

Route::get('user/{id}/{name}', function ($id ,$name) {
    return 'User Id '.$id.' User Name '.$name;
});


3.Pass below url in google chrome to see above route output
http://localhost/blog/public/user/1/Shiva

Related Post