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:
Below are handy list of patterns that you can reuse them everywhere:
Route::pattern('id', '\d+');
Route::pattern('hash', '[a-z0-9]+');
Route::pattern('hex', '[a-f0-9]+');
Route::pattern('uuid', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}');
Route::pattern('base', '[a-zA-Z0-9]+');
Route::pattern('slug', '[a-z0-9-]+');
Route::pattern('username', '[a-z0-9_-]{3,16}');
make more of your own to suit your needs: email, password
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...
});