定制 aesircloud/sluggable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 aesircloud/sluggable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 172
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-27