承接 philiprehberger/laravel-slug-generator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

philiprehberger/laravel-slug-generator

最新稳定版本:v1.1.0

Composer 安装命令:

composer require philiprehberger/laravel-slug-generator

包简介

Automatic slug generation for Eloquent models with scoped uniqueness, history, and transliteration

README 文档

README

Tests Latest Version on Packagist Last updated

Automatic slug generation for Eloquent models with scoped uniqueness, history, and transliteration.

Requirements

  • PHP 8.2+
  • Laravel 11 or 12

The PHP intl extension is recommended for best transliteration results (falls back to iconv then a simple strip).

Installation

composer require philiprehberger/laravel-slug-generator

The service provider is auto-discovered via Laravel's package discovery. No manual registration is required.

Publish configuration

php artisan vendor:publish --tag=slug-generator-config

This creates config/slug-generator.php in your application.

Publish and run the slug history migration (optional)

Only required if you intend to use the HasSlugHistory trait.

php artisan vendor:publish --tag=slug-generator-migrations
php artisan migrate

Usage

Basic Usage

Add the HasSlug trait to any Eloquent model:

use Illuminate\Database\Eloquent\Model;
use PhilipRehberger\SlugGenerator\Concerns\HasSlug;

class Post extends Model
{
    use HasSlug;
}

The trait reads from the title column by default and writes to slug:

$post = Post::create(['title' => 'Hello World']);
echo $post->slug; // 'hello-world'

Duplicate slugs automatically receive a numeric suffix:

Post::create(['title' => 'Hello World']); // slug: 'hello-world'
Post::create(['title' => 'Hello World']); // slug: 'hello-world-2'

Per-Model Overrides

class Article extends Model
{
    use HasSlug;

    public function slugSource(): string|array  { return 'title'; }
    public function slugField(): string          { return 'slug'; }
    public function slugSeparator(): string      { return '-'; }
    public function slugMaxLength(): ?int        { return null; }
    public function slugShouldBeUnique(): bool   { return true; }
    public function slugUniqueScope(): ?string   { return null; }
    public function slugOnUpdate(): bool         { return false; }
}

Scoped Uniqueness

class Post extends Model
{
    use HasSlug;

    public function slugUniqueScope(): ?string
    {
        return 'category_id';
    }
}

Slug Template

Use a template pattern to control how attributes are combined in the slug:

class Author extends Model
{
    use HasSlug;

    public function slugTemplate(): ?string
    {
        return '{last_name}-{first_name}';
    }
}

$author = Author::create(['first_name' => 'John', 'last_name' => 'Doe']);
echo $author->slug; // 'doe-john'

Placeholders use the {attribute} syntax and are resolved from model attributes before slugification. Missing or null attributes are omitted from the result.

Slug History and Redirects

use PhilipRehberger\SlugGenerator\Concerns\HasSlug;
use PhilipRehberger\SlugGenerator\Concerns\HasSlugHistory;

class Post extends Model
{
    use HasSlug;
    use HasSlugHistory;

    public function slugOnUpdate(): bool { return true; }
}

Use findBySlugOrRedirect() in controllers to handle current and old slugs transparently:

public function show(string $slug): Response
{
    $result = Post::findBySlugOrRedirect($slug);

    if ($result === null) { abort(404); }

    if (is_array($result) && $result['redirect']) {
        return redirect(route('posts.show', $result['slug']), 301);
    }

    return view('posts.show', ['post' => $result]);
}

API

HasSlug Trait — Override Methods

Method Return Type Default Description
slugSource() string|array 'title' Source column(s) to generate slug from
slugField() string 'slug' Database column to store the slug
slugSeparator() string '-' Word separator
slugMaxLength() ?int null Max length; truncates at word boundary
slugShouldBeUnique() bool true Enforce unique slugs
slugUniqueScope() ?string null Column to scope uniqueness checks
slugTemplate() ?string null Template pattern with {attribute} placeholders
slugOnUpdate() bool false Regenerate slug on model update

HasSlugHistory Trait

Method Description
Post::findBySlugOrRedirect(string $slug) Returns model, redirect array, or null
->slugHistories MorphMany relationship to slug history records

SlugRedirectMiddleware Parameters

Position Name Description
1 modelClass Fully-qualified model class (must use HasSlugHistory)
2 routeParam Route parameter name that holds the slug (default: slug)
3 urlPrefix URL prefix for the redirect target (default: /)

Development

composer install
vendor/bin/phpunit
vendor/bin/pint --test
vendor/bin/phpstan analyse

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固