Your cart is currently empty!
how to route model binding work?
Hi guys,
Today we are studying Advanced Laravel Routing. In the last article, we studied basic knowledge of Routing in Laravel. Stay connected!
Route Model binding:
It is a convenient way to automats inject model instances directly into your routes.
Implicit binding:
Laravel resolves an eloquent model defined in routes or controller actions and it matches a route segment name. For example,
Route::get('users/{user}', function(App\User $user){
return $user->email;
})
Customizing the key name:
You can do any operation using any other field than id. So You must override the getRouteKeyName method on the model.
public function getRouteKeyName()
{
return 'slug';
}
Explicit binding:
Add Route::model() function with a model in the boot method of RouteServiceProvider. For example:
public function boot()
{
parent::boot();
Route::model('user', App\User::class);
}
Afterward, You use the user parameter in any route so you will access the user model.
Route::get('profile/{user}',function(App\User $user){
})
Fallback Route:
You may define a fallback route within your web.php route. All middleware in the web middleware group will apply to the route. You are free to add additional middleware to this route as needed. The Fallback Route will register after all Routes get registered.
Route::fallback(function(){
});
Rate Limiting:
throttle middleware assigned to the group of routes. It accepts two parameters one number of requests and the second minute per request handle. For example, let’s say login users must have to access 60 requests per minute.
Route::middleware('auth:api','throttle:60,1')->group(function(){
Route::get('/customers',function(){
})
})
According to user type, you can assign a dynamic request count using the rate_limit attribute on the User model. You can pass rate_limit to middleware.
Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function(){
})
Also, you can assign a different rate limit for guests and authenticated users.
Route::middleware('auth:api','throttle:10|60,1')->group(function(){
})
A guest has ’10’ requests per minute, and an authenticated user has ’60’ requests per minute. The user model contains a rate_limit attribute. You may pass the name of the attribute to the throttle middleware. So that the maximum request count should be different as per the user.
Route::middleware('auth:api','throttle:10|rate_limit,1')->group(function(){
})
Rate limiting segment:
Your application may require a different rate limit for different Segments of your API. you may pass the third argument as a segment name to the throttle middleware.
Route::middleware('auth:api','throttle:60,1,default')->group(function(){
})
HTML Method Spoofing:
HTML does not support PUT, DELETE, and PATCH. you need to add a hidden field to the HTML form. The value sent with the _method field will be used as an HTTP request method.
You may also use @method So Blade will generate the _method input field. Add the below code to the form
@method('PUT')
@csrf
Accessing The Current Route:
You can access the current route, route name, and route action by using the ‘current’,’currentRouteName’, and ‘currentRouteAction’ methods from Route Facades.
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
In this article, you have learned route model binding i.e. implicit and explicit binding Also you can rate limit requests.
Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers,