Laravel - Routing - 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 App\Providers\RouteServiceProvider class:


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

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...
});