Regular Expression Constraints

 Regular  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:

Example:

 Route::get('/user/{name}', function ($name) {
        return "Letter Route Working with Upper Case & Lower Case";
    })->where('name', '[A-Za-z]+');

O/P
Example:
     
    Route::get('/user/{id}', function ($id) {
         return 'Route Only Numeric :' . $id;
    })->where('id','[0-9]+' );

O/P

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


O/P


Comments