YouTube Icon

Code Playground.

Global Constraints Route in Laravel 5.7

CFG

Global Constraints Route in Laravel 5.7

You may also use pattern method to constrain global constraints route parameter to be constrained by a given regular expression. Use two methods, one is boot and the other is RouteServiceProvider, you may need to identify patterns.

Let see in below example.

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

    parent::boot();
}

Once the pattern has been defined, All route to automatic applied using that parameter name.

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

Encoded Forward Slashes

This routing component allows all characters except /.

Note: Encoded forward slashes are only supported within the last route segment.

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

And if you like this tutorials please share it with your friends via Email or Social Media.




CFG