into-the-source / laravel-slugable
Laravel 可生成短网址的特性
2.0
2024-09-11 16:04 UTC
Requires
- laravel/framework: ^6.0
This package is not auto-updated.
Last update: 2024-09-26 12:49:17 UTC
README
此包会根据默认的 title
自动确定短网址
或者通过在模型中指定的 slugableColumn
变量值来指定。
注意:此包会尽量不使用旧的短网址,因为旧链接可能会链接到错误的结果,同时有利于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'
如果标题以数字结尾,它将直接在现有短网址后添加后缀。例如
'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'