mimachh / slugme
将您需要的任何模型进行slug化
Requires
- laravel/framework: ^11.9
README
使用方法:两种方式
- 一个简单的函数,可以从数据生成slug并返回slug。
此包不会提供迁移。您需要手动添加列。
Slugme
Slugme
是一个简单且高效的Laravel包,可以帮助您自动为模型的指定属性生成唯一的slug并将其存储在数据库中。
安装
要使用Composer安装此包,请运行以下命令
composer require mimachh/slugme
用法
重要:手动添加 slug
列
请注意,必须在您的数据库迁移中手动添加 slug
列。
在您的模型中实现 Sluggable 接口
要使用此包,您需要实现您想要进行slug化的模型的 Sluggable 接口。
use Mimachh\Slugme\Contracts\Sluggable; use Mimachh\Slugme\Concerns\HasSlug; use Illuminate\Database\Eloquent\Model; class Post extends Model implements Sluggable { use HasSlug; // Define the column to store the slug public function slugColumn(): string { return 'slug'; } // Define the attribute to generate the slug from public function slugAttribute(): string { return 'title'; } }
自动生成 Slugs
一旦您实现了 Sluggable 接口并使用了 HasSlug 特性,在创建或更新模型时,将自动生成slug。该包确保即使存在冲突,slug也是唯一的。
例如,如果标题是“我的第一篇文章”,则该包将生成一个slug如“my-first-post”。如果slug已存在,它将附加一个计数器(例如,“my-first-post-1”)。
在模型更新时更新 Slugs
如果您更新了slugAttribute(例如,标题),则该包将自动重新生成并确保slug保持唯一。
自定义 Slug 逻辑
如有需要,您还可以通过在模型中重写 generateUniqueSlug 方法来自定义slug生成逻辑。
public static function generateUniqueSlug(string $attribute): string { // Custom slug generation logic here }
示例:以下是如何创建和更新具有slug的模型的示例
$post = new Post(); $post->title = 'My Awesome Post'; $post->save(); // This will automatically generate a slug and save it in the `slug` column. $post->title = 'My Updated Awesome Post'; $post->save(); // This will update the slug accordingly.
不实现 Sluggable
接口生成唯一 Slugs
如果您不想实现 Sluggable
接口并希望手动处理slug生成,可以使用此包包含的 SlugGenerator
服务。此方法提供直接调用slug生成方法并将它保存到数据库中的灵活性。
1. 使用 SlugGenerator
服务
SlugGenerator
类允许您为任何模型和属性生成唯一的slug,同时确保slug在模型更新期间也是唯一的。
以下是使用方法。
创建 Slugs 的示例
在这个例子中,我们将根据 title
属性为 Post
模型生成唯一的slug。
use Mimachh\Slugme\Services\SlugGenerator; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $fillable = ['title', 'slug']; public static function boot() { parent::boot(); // Automatically generate a unique slug when creating a new model static::creating(function ($post) { $post->slug = SlugGenerator::generateUniqueSlug($post->title, Post::class); }); // Automatically regenerate the slug if the title is updated static::updating(function ($post) { if ($post->isDirty('title')) { $post->slug = SlugGenerator::generateUniqueSlug($post->title, Post::class, $post->id); } }); } }
在这个例子中
当创建新的 Post 时,创建事件会自动使用标题属性生成slug。在更新时,如果标题已被修改,更新事件会重新生成slug并确保唯一性,排除当前文章的ID。
2. 手动生成 Slugs
您也可以在控制器或应用程序的任何地方手动生成slugs。这使您能够控制何时以及在哪里生成和保存slugs。
use Mimachh\Slugme\Services\SlugGenerator; // Creating a new post with manual slug generation $post = new Post(); $post->title = 'My Awesome Post'; $post->slug = SlugGenerator::generateUniqueSlug($post->title, Post::class); // Generate slug manually $post->save();
对于更新
$post = Post::find(1); $post->title = 'Updated Post Title'; $post->slug = SlugGenerator::generateUniqueSlug($post->title, Post::class, $post->id); // Pass the current ID to exclude it $post->save();
3. SlugGenerator
解释
SlugGenerator::generateUniqueSlug
方法接受以下参数
string $attribute
:您希望生成slug的属性(例如,标题)。string $modelClass
:要检查slug唯一性的模型类(例如,Post::class)。int|null $currentId
:(可选) 当前模型的ID,用于在更新时排除此记录。string $slugColumn
:(可选) 存储slug的数据库列(默认为slug)。
迁移示例
如前所述,请确保在迁移中添加slug列,如下所示
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->string('slug')->unique(); // Add this column $table->timestamps(); }); } public function down() { Schema::dropIfExists('posts'); } }
采用这种方法,您可以对slug生成和唯一性检查拥有完全控制,无需实现Sluggable接口或使用特性。