Home  >  Article  >  PHP Framework  >  About simple implicit routing model binding in Laravel 7

About simple implicit routing model binding in Laravel 7

藏色散人
藏色散人forward
2020-04-07 09:06:032521browse

About simple implicit routing model binding in Laravel 7

In the next major release of Laravel, you can customize implicit routing model binding directly in the routing definition:

Recommended: laravel tutorial

Route::get('/posts/{post:slug}', function (Post $post) {
    // ...
});

Currently, using Laravel 6, the requirements below require you to define a getRouteKeyName() method on the model like this:

<?php
class Post extends Model
{
    /**
     * Get the route key for the model.
     *
     * @return string
     */
    public function getRouteKeyName()
    {
        return &#39;slug&#39;;
    }
}

You can still use the getRouteKeyName() method; however , I think it would be smoother to customize it directly in the route.

Maybe you will have multiple routes that you want to bind in different ways. For example, the frontend router uses slugs to display posts, and the backend hopes to manage posts by ID

Route::get(&#39;/posts/{post:slug}&#39;, function (Post $post) {
    // ...
});
// 或者你在这儿可以用默认的`{post}`
Route::get('/admin/posts/{post:id}/edit', function (Post $post) {
    // ...
});

If you start trying to customize implicit routing model binding, you can install the development version of Laravel

laravel new example --dev

The article is forwarded from the professional Laravel developer community, original link: https://learnku.com/laravel/t/37702

The above is the detailed content of About simple implicit routing model binding in Laravel 7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete