aesircloud/sluggable
Composer 安装命令:
composer require aesircloud/sluggable
包简介
A Laravel package to automatically generate slugs for models
关键字:
README 文档
README
sluggable is a Laravel package that generates unique slugs for Eloquent models. It can be used to automatically generate slugs when creating or updating models, with flexible options controlled by both a config file and model-level properties.
Installation
You can install the package via Composer:
composer require aesircloud/sluggable
Publish the configuration file
php artisan vendor:publish --provider="AesirCloud\Sluggable\SluggableServiceProvider"
Usage
Add the Sluggable trait to your model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use AesirCloud\Sluggable\Traits\Sluggable; class Post extends Model { use Sluggable; protected $fillable = ['title', 'slug']; /** * Optionally override the default slug source field. * * By default, the package uses whatever is set in 'sluggable.source' * or falls back to 'name' on your model. Here, we explicitly use 'title'. */ protected $slugSource = 'title'; /** * If you want to allow the slug to be updated automatically when 'title' changes: */ protected $slugUpdatable = true; /** * If you want a different slug column in your database table: */ protected $slugColumn = 'url_slug'; }
2. Migrate the Slug Column
Ensure your table has a suitable column for the slug:
php artisan make:migration add_slug_to_posts_table --table=posts
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::table('posts', function (Blueprint $table) { // Make sure it's unique or at least indexed if you rely on uniqueness. $table->string('slug')->unique()->after('title'); }); } public function down(): void { Schema::table('posts', function (Blueprint $table) { $table->dropColumn('slug'); }); } };
3. Configuration Options
Open config/sluggable.php to see the available options. You can override any of these in your model by defining the corresponding property.
return [ 'source' => 'name', 'column' => 'slug', 'update' => false, 'max_length' => 255, 'scopes' => [], // e.g. ['category_id'] to scope uniqueness ];
- source: The default field the slug is generated from if you don’t set $slugSource in the model.
- column: The column to store the slug.
- update: Set true to automatically regenerate a slug when the source field changes on update.
- max_length: Truncates the slug (plus space for numeric suffixes) to this length if specified.
- scopes: Array of columns to scope uniqueness (e.g., if you only want unique slugs per category_id).
4. Route Model Binding (Optional)
If you want to use the slug in your routes, you can override getRouteKeyName() in your model:
public function getRouteKeyName() { return 'slug'; }
Then reference it in your routes:
Route::get('/posts/{post:slug}', function (App\Models\Post $post) { return $post; });
Changelog
Please see CHANGELOG for more information what has changed recently.
Security
If you've found a bug regarding security please mail security@aesircloud.com instead of using the issue tracker.
LICENSE
The MIT License (MIT). Please see License file for more information.
aesircloud/sluggable 适用场景与选型建议
aesircloud/sluggable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 172 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sluggable」 「slug」 「AesirCloud」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aesircloud/sluggable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aesircloud/sluggable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aesircloud/sluggable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
TitleWithSlugInput - Easy Permalink Slugs for the FilamentPHP Form Builder (PHP / Laravel / Livewire)
Provides a HasSlug trait that will generate a unique slug when saving your Laravel Eloquent model.
Adds slugify twig extensions to your craft install.
A simple Cake3 plugin to slug fields and find records by slug.
A library to extend Object capabilities.
Extend laravel applications with more artisan commands, traits and useful helper classes
统计信息
- 总下载量: 172
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-27