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

About simple implicit routing model binding in Laravel 7

藏色散人
Release: 2020-04-07 09:06:03
forward
2564 people have browsed it

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) {
    // ...
});
Copy after login

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;;
    }
}
Copy after login

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) {
    // ...
});
Copy after login

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

laravel new example --dev
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template