The Routing model binding feature has been provided in Laravel5.1, and in Laravel5.2, this functionality has been made simpler.
1. Basic use of routing model bindings
Typically we bind the model in URL routing in the following way:
Route::group ([' middleware ' = = [' web ']], function () { route::get ('/user/{id} ', function ($id) { $user = \app\ User::findorfail ($id); DD ($user); });
I usually do so, but wouldn't it be better if there was a way to get rid of the Findorfail line and fetch $user instance directly? In fact, this is true in Laravel, where the following line of code is added to the boot method in the routing service provider Routeserviceprovider:
Public Function boot (Router $router) { $router->model (' user ', user::class); Parent::boot ($router);}
This means that whenever the route contains the parameter user and its ID value corresponds to a app\user instance, this mechanism allows us to rewrite the code as follows:
Route::group ([' middleware ' + = [' web ']], function () { route::get ('/user/{user} ', function ($user) { DD ($ user);}) ;
2. Implicit Routing model binding
In Laravel 5.2, it is simpler to use the routing model binding, simply by declaring the parameter in the routing closure (or Controller method) and keeping the parameter consistent with the routing parameter, so that the parameter is automatically processed as a route model binding (no longer need to Call the model method in Routeserviceprovider to declare the binding relationship):
Route::group ([' middleware ' + = [' web ']], function () { route::get ('/user/{user} ', function (\app\user $user) { DD ($user); });
The print results are exactly the same as before.
3. Some characteristics of routing model bindings
3.1 Custom Routing Model binding logic
If you want to customize the routing model binding logic to return the required instances, you can pass a closure instead of the class name as an explicit binding (relative implicit model binding, which needs to define the second parameter of the binding relationship called explicit binding in the Routeserviceprovider boot method):
Public Function boot (Router $router) { $router->bind (' User ', function ($value) { return user::where (' name '), $value)->where (' status ', 1)->first (); }); Parent::boot ($router);}
3.2 Custom route Model binding exception
You can also customize the exception that is thrown by the routing model binding by passing a closure as the third parameter (such as when the corresponding model instance is not found):
Public Function boot (Router $router) { $router->bind (' User ', User::class,function () { throw new notfoundhttpexception; }); Parent::boot ($router);}
3.3 Modifying the "route key" of the eloquent model
By default, Laravel uses the ID field in the URL fragment to match the eloquent model, but what if you want to customize this field just like the custom route model bindings above?
Eloquent implements the Illuminate\contracts\routing\urlroutable interface, which means that the eloquent object has a getroutekeyname () method, In this method, you can define which field in the URL to use to match the model instance, the default is the ID, and you can override it in any eloquent model:
Class User extends Model {public function getroutekeyname () { return ' name '; }}
You can now use either explicit or implicit routing model bindings, and you can customize the matching fields in the URL fragment.