Route Parameters: Regular Expression Constraints

Syntax

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

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

    parent::boot();
}

/* web.php
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...
});
Route::get('user/{id}', function ($id) {
    // Only executed if {id} is numeric...
});

Route Parameters: Regular Expression Constraints — Structure map

Clickable & Draggable!

Route Parameters: Regular Expression Constraints — Related pages: