intothesource/laravel-slugable

laravel的slugable特性

1.0 2019-07-26 13:08 UTC

This package is not auto-updated.

Last update: 2024-09-19 08:54:50 UTC


README

此包默认会根据 title 自动确定slug
或者可以通过在模型中指定 slugableColumn 变量的值来指定。

注意:此包会尝试永远不使用旧的slug,因为SEO友好性和旧的链接可能会链接到错误的结果。

用法

use Slugable 添加到您的模型中,例如以下示例

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Intothesource\Slugable\Slugable;

class Page extends Model
{
    use Slugable;
    
    ...

此特性包含一个 setSlugAttribute 方法,您只需从控制器或使用事件监听器触发它即可。
但这一切都取决于您。

示例

'This is a title' => 'this-is-a-title'

如果您再次提交相同的标题,它将返回

'This is a title' => 'this-is-a-title-2'

如果标题以数字结尾,它将仅向现有的slug添加后缀。例如

'This is my post number 20' => 'this-is-my-post-number-20'

第二次

'This is my post number 20' => 'this-is-my-post-number-20-2'

致谢