如何使用弹头的URL

问题描述 投票:0回答:1

我试图用蛞蝓的方式来显示我的URL链接。例如,而不是使用:

http://localhost:8000/product/1

我想用...

http://localhost:8000/product/blue-shirts

到目前为止,我设法通过封装cviebrock/eloquent-sluggable添加蛞蝓。我应该用我的路线和看法?

<?php

use Sluggable;

public function sluggable()
{
    return [
        'slug' => [
            'source' => 'product_name'
        ]
    ];
}
laravel laravel-routing slug
1个回答
0
投票

您可以自定义路径模型,在RouteServiceProvider约束力的决议逻辑:

public function boot()
{
    parent::boot();

    Route::bind('product', function ($value) {
        return Product::where('slug', $value)->first() ?? abort(404);
    });
}

the docs here以获取更多信息。

另外,以下内容添加到您的Product模型使用隐式绑定:

/**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'slug';
}

Package docs explain further here

© www.soinside.com 2019 - 2024. All rights reserved.