elstc/cakephp-slug-guard
Composer 安装命令:
composer require elstc/cakephp-slug-guard
包简介
CakePHP plugin for URL-safe slug validation and reserved-word collision prevention
关键字:
README 文档
README
A CakePHP plugin that ensures user-chosen slugs are safe and conflict-free for use in URLs.
Version Map
| CakePHP | PHP | Plugin | Branch |
|---|---|---|---|
| 5.x | >= 8.2 | 5.x | cake5 |
- SlugValidator validates that a string is well-formed for use as a subdomain label or URL path segment (lowercase alphanumeric + hyphens, length constraints).
- IsNotReservedSlug compares slugs against a reserved-word list so they never collide with system routes or well-known paths.
- IsNotRouteConflict compares slugs against first-level URL segments extracted from the CakePHP route table to prevent collisions with application routes.
Installation
Prerequisites
- A CakePHP 5.x application
- PHP >= 8.2
- A database connection configured in your app (
config/app_local.php)
Steps
-
Install the plugin via Composer:
composer require elstc/cakephp-slug-guard
-
Load the plugin. This adds
$this->addPlugin('Elastic/SlugGuard')to yoursrc/Application.php:bin/cake plugin load Elastic/SlugGuard
-
Run the migration to create the
reserved_slugstable:bin/cake migrations migrate --plugin Elastic/SlugGuard
-
Import the default reserved slugs (~710 common reserved words):
bin/cake slug_guard sync
Verify Installation
Run the following command to confirm the reserved slugs were imported:
bin/cake slug_guard list --count
Expected: a count of approximately 710 reserved slugs.
Usage
Validation Rule
Add the IsNotReservedSlug rule to your table's buildRules() method:
use Elastic\SlugGuard\Model\Rule\IsNotReservedSlug; public function buildRules(RulesChecker $rules): RulesChecker { $rules->add(new IsNotReservedSlug('slug'), 'reservedSlug', [ 'errorField' => 'slug', 'message' => 'This slug is reserved.', ]); return $rules; }
Custom field name
$rules->add(new IsNotReservedSlug('username'), 'reservedSlug', [ 'errorField' => 'username', 'message' => 'This username is reserved.', ]);
Custom reserved slugs table
You can use any table as the reserved slugs backend by implementing SlugExistenceInterface:
// src/Model/Table/CustomReservedSlugsTable.php namespace App\Model\Table; use Cake\ORM\Table; use Elastic\SlugGuard\Model\Table\SlugExistenceInterface; class CustomReservedSlugsTable extends Table implements SlugExistenceInterface { public function slugExists(string $slug): bool { return $this->exists(['slug' => $slug]); } }
Then pass the table name to IsNotReservedSlug:
$rules->add(new IsNotReservedSlug('slug', 'CustomReservedSlugs'), 'reservedSlug', [ 'errorField' => 'slug', 'message' => 'This slug is reserved.', ]);
Route Conflict Rule
Add the IsNotRouteConflict rule to prevent slugs from colliding with application routes (e.g. /admin, /api, /posts):
use Elastic\SlugGuard\Model\Rule\IsNotRouteConflict; public function buildRules(RulesChecker $rules): RulesChecker { $rules->add(new IsNotRouteConflict('slug'), 'routeConflict', [ 'errorField' => 'slug', 'message' => 'This slug conflicts with an application route.', ]); return $rules; }
This rule extracts first-level URL path segments from all registered CakePHP routes at runtime and rejects any slug that matches.
Slug Format Validator
The SlugValidator class provides a static method for validating slug format (lowercase alphanumeric and hyphens):
use Elastic\SlugGuard\Validation\SlugValidator; // Use as a Validator provider $validator->setProvider('slugValidator', SlugValidator::class); $validator->add('slug', 'validSlug', [ 'rule' => ['isValid'], 'provider' => 'slugValidator', 'message' => 'Only lowercase letters, numbers, and hyphens are allowed.', ]);
Configuration Options
minLength
Minimum slug length. Default: 4
maxLength
Maximum slug length. Default: 24
$validator->add('slug', 'validSlug', [ 'rule' => ['isValid', 3, 32], 'provider' => 'slugValidator', ]);
CLI Commands
List reserved slugs
bin/cake slug_guard list bin/cake slug_guard list --count bin/cake slug_guard list --search admin
List route paths
bin/cake slug_guard routes bin/cake slug_guard routes --count
Output is pipe-friendly (one path per line, no decoration). See Route-based workflow below.
Add reserved slugs
bin/cake slug_guard add my-reserved-slug bin/cake slug_guard add slug-one slug-two slug-three
Supports stdin for pipe usage:
bin/cake slug_guard routes | bin/cake slug_guard add
Remove reserved slugs
bin/cake slug_guard remove my-reserved-slug bin/cake slug_guard remove slug-one slug-two slug-three
Also supports stdin:
cat slugs-to-remove.txt | bin/cake slug_guard remove
Import slugs from a file
bin/cake slug_guard import /path/to/slugs.txt
File format: one slug per line, # for comments, empty lines are ignored.
Sync with a file
# Sync (auto-detects app config file or falls back to plugin built-in) bin/cake slug_guard sync # Sync with a specific file bin/cake slug_guard sync --file /path/to/slugs.txt # Preview changes without applying bin/cake slug_guard sync --dry-run
When --file is not specified, the sync command resolves the seed file in the following order:
- Application config file:
config/reserved-slugs.txt(in app root) - Plugin built-in seed file
You can override the application config file path via Configure:
// In config/app.php or config/app_local.php 'SlugGuard' => [ 'syncFile' => CONFIG . 'my-custom-slugs.txt', ],
Reserved Slugs List File
The plugin ships with a default reserved slugs list at config/reserved-slugs.txt. This file contains ~710 common reserved words (e.g. admin, api, login, settings, www) that could conflict with system routes or well-known paths.
File format
- One slug per line
- Lines starting with
#are comments - Empty lines are ignored
# System routes
admin
api
login
# Social media
facebook
twitter
youtube
Using the built-in list
The built-in list is used as a fallback by the sync command when no app-level config file exists. You can also import it directly:
bin/cake slug_guard import vendor/elstc/cakephp-slug-guard/config/reserved-slugs.txt
Using a custom list
Create your own file following the same format and use it with import or sync:
# Import additional slugs from a custom file bin/cake slug_guard import /path/to/my-slugs.txt # Sync the database to match your custom file exactly bin/cake slug_guard sync --file /path/to/my-slugs.txt
Note:
importadds slugs from the file to the database (existing slugs are preserved).syncmakes the database match the file exactly — slugs not in the file will be removed.
Route-based Workflow
The slug_guard routes command extracts first-level URL path segments from your application's route table. Use it to automatically reserve paths that would conflict with user-generated slugs.
Pipe directly to the database
bin/cake slug_guard routes | bin/cake slug_guard add
Append to your reserved slugs file, then sync
bin/cake slug_guard routes >> config/reserved-slugs.txt
bin/cake slug_guard sync
elstc/cakephp-slug-guard 适用场景与选型建议
elstc/cakephp-slug-guard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 393 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「cakephp」 「slug」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 elstc/cakephp-slug-guard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 elstc/cakephp-slug-guard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 elstc/cakephp-slug-guard 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
TitleWithSlugInput - Easy Permalink Slugs for the FilamentPHP Form Builder (PHP / Laravel / Livewire)
Adds slugify twig extensions to your craft install.
A library to extend Object capabilities.
Email Toolkit Plugin for CakePHP
A Laravel Filament Forms slug field.
统计信息
- 总下载量: 393
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 42
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-25