jetcod/laravel-slugify
Composer 安装命令:
composer require jetcod/laravel-slugify
包简介
A Laravel package that automatically generates slugs for model attributes, simplifying the creation of URL-friendly slugs.
关键字:
README 文档
README
Overview
The jetcod\laravel-slugify package simplifies the generation and management of slugs for Eloquent models in Laravel applications. This package utilizes the HasSlug trait to automatically create and update slugs based on your model's attributes, with flexible configuration options.
Installation
Requirements
- PHP ^7.4 | ^8.0
- Laravel 8.0+
Step 1: Install via Composer
To install the package, run the following command:
composer require jetcod/laravel-slugify
Step 2: Configure your model
In any model where you want to use the slugging functionality, use the HasSlug trait and implement getSlugConfig() method to configure the slug options.
Usage
Setting Up the Sluggable Model
To start using slugs in your model, follow these steps:
- Use the Trait: In your model, use the
HasSlugtrait. - Define Slug Configuration: Implement the
getSlugConfig()method in your model. This method should return a SlugOptions object where you define your sluggable configuration. - Set the
$sluggablesproperty: Define an array of model attributes that should be used to generate the slug.
Here’s an example:
use Jetcod\LaravelSlugify\SlugOptions; use Jetcod\LaravelSlugify\Traits\HasSlug; class YourModel extends Model { use HasSlug; protected $casts = [ 'slugs' => 'array', // Optional: Cast the 'slugs' attribute to an array ]; protected $sluggables = ['a_column_name']; // Specify columns to be sluggified protected function getSlugConfig(): SlugOptions { return SlugOptions::create() ->slugColumn('slugs') // Define the column name where the slugs will be stored ; } }
Available Configuration Options
- doNotGenerateSlugsOnCreate(): Call this method if you want to avoid generating slugs on model creation.
- doNotGenerateSlugsOnUpdate(): Call this method if you want to avoid generating slugs when the model is updated.
- slugColumn(string): Define the column name where the slugs will be stored. The defined column type should be
json. - slugSeparator(string): Character separator between words in the slug. (Default value is '-')
- maximumLength(int): Maximum character length of the slug.
- avoidDuplicates(): Call this method to generate unique slugs.
Note: Calling slugColumn() is required while defining the slug options.
Example: Creating and Updating a Model with Slug Generation
When you create or update a model with HasSlug configured, the slug is automatically generated and saved according to the options you've specified.
$post = new Post(); $post->title = "This is an Example Title"; $post->save(); // $post->slugs will be like {"title":"this-is-an-example-title"} (based on the configured options)
Example: Generating Unique Slugs
To ensure unique slugs, you can use the avoidDuplicates() method in your SlugOptions:
use Jetcod\LaravelSlugify\SlugOptions; use Jetcod\LaravelSlugify\Traits\HasSlug; class YourModel extends Model { use HasSlug; protected $sluggables = ['title']; protected function getSlugConfig(): SlugOptions { return SlugOptions::create() ->slugColumn('slugs') ->avoidDuplicates() ; } }
Then when you create or update a model, the slug will be generated and saved as a unique value.
$post = new Post(); $post->title = "This is an Example Title"; $post->save(); // Result: $post->slugs will be like {"title":"this-is-an-example-title"} // Create another post with the same title $post = new Post(); $post->title = "This is an Example Title"; $post->save(); // Result: $post->slugs will be like {"title":"this-is-an-example-title-1"}
Note: It also considers the maximum length of the slug while generating unique slug strings.
Testing
Run your tests to verify that slugs are generated as expected:
composer test
Run code coverage analysis to generate a coverage report. This will generate a coverage report in the coverage directory.
composer coverage
Run PHPStan to check for potential issues in the code:
composer phpstan
License
This package is open-source software licensed under the MIT License.
jetcod/laravel-slugify 适用场景与选型建议
jetcod/laravel-slugify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 133 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「url」 「slug」 「slugify」 「laravel」 「url-friendly」 「url-slug」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jetcod/laravel-slugify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jetcod/laravel-slugify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jetcod/laravel-slugify 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A full-featured, simple, clean and pure functional implementation for creating slugs
TitleWithSlugInput - Easy Permalink Slugs for the FilamentPHP Form Builder (PHP / Laravel / Livewire)
A Symfony2 Bundle to slugify swedish texts
Adds slugify twig extensions to your craft install.
Slug Generator
A library to extend Object capabilities.
统计信息
- 总下载量: 133
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-25