roukmoute/sqids-bundle
Composer 安装命令:
composer require roukmoute/sqids-bundle
包简介
Integrates sqids/sqids in a Symfony project
README 文档
README
Integrates Sqids into your Symfony project.
Sqids (pronounced "squids") generates short unique identifiers from numbers. These IDs are URL-safe, can encode several numbers, and do not contain common profanity words.
This is the official successor to Hashids. If you are starting a new project, use Sqids instead of Hashids.
Installation
composer require roukmoute/sqids-bundle
If you're using Symfony Flex, the bundle will be automatically registered. Otherwise, add it to your config/bundles.php:
return [ // ... Roukmoute\SqidsBundle\RoukmouteSqidsBundle::class => ['all' => true], ];
Configuration
Create config/packages/roukmoute_sqids.yaml:
roukmoute_sqids: alphabet: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' # Custom alphabet min_length: 0 # Minimum length of generated sqids blocklist: null # Path to a JSON file containing blocked words, or null to use defaults passthrough: false # Pass decoded value to next resolver (for Doctrine integration) auto_convert: false # Automatically decode route parameters matching int arguments
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
alphabet |
string | Default Sqids alphabet | Characters used to generate sqids. Must contain at least 3 unique characters. |
min_length |
int | 0 | Minimum length of generated sqids. |
blocklist |
string|null | null | Path to a JSON file containing words to avoid in generated sqids. |
passthrough |
bool | false | When true, sets the decoded value in request attributes for the next resolver (useful with Doctrine). |
auto_convert |
bool | false | When true, automatically attempts to decode any route parameter matching an int argument name. |
Usage
Basic Encoding/Decoding
Inject SqidsInterface into your service or controller:
use Sqids\SqidsInterface; class MyService { public function __construct(private SqidsInterface $sqids) { } public function encode(int $id): string { return $this->sqids->encode([$id]); } public function decode(string $sqid): int { $decoded = $this->sqids->decode($sqid); return $decoded[0]; } }
Controller Argument Resolution
Using the #[Sqid] Attribute
The #[Sqid] attribute automatically decodes sqid route parameters:
use Roukmoute\SqidsBundle\Attribute\Sqid; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; class UserController { #[Route('/users/{id}')] public function show(#[Sqid] int $id): Response { // $id is automatically decoded from the sqid in the URL // e.g., /users/UK → $id = 1 } }
Custom Parameter Name
When the route parameter name differs from the argument name:
#[Route('/users/{sqid}')] public function show(#[Sqid(parameter: 'sqid')] int $id): Response { // The 'sqid' route parameter is decoded into $id }
Using Aliases
Without any attribute, the resolver recognizes these aliases:
{sqid}route parameter{id}route parameter
#[Route('/users/{id}')] public function show(int $userId): Response { // If {id} contains a valid sqid, $userId receives the decoded value }
Note: Aliases are consumed once per request to avoid ambiguous resolution with multiple arguments.
Multiple Sqids in a Single Route
Use the _sqid_ prefix to decode multiple parameters:
#[Route('/users/{_sqid_user}/posts/{_sqid_post}')] public function showPost(int $user, int $post): Response { // Both $user and $post are decoded from their respective sqids }
Auto-Convert Mode
Enable auto_convert: true in your configuration to automatically attempt decoding all route parameters that match int typed controller arguments:
roukmoute_sqids: auto_convert: true
#[Route('/users/{id}')] public function show(int $id): Response { // $id is automatically decoded if it looks like a valid sqid // If decoding fails, the resolver silently skips (no exception) }
Passthrough Mode (Doctrine Integration)
Enable passthrough: true to chain with Doctrine's EntityValueResolver:
roukmoute_sqids: passthrough: true
use App\Entity\User; #[Route('/users/{id}')] public function show(User $user): Response { // The sqid is first decoded, then Doctrine fetches the User entity }
The resolver decodes the sqid and sets the integer value in the request attributes, allowing Doctrine's resolver to find the entity.
Twig Integration
The bundle provides Twig filters and functions for encoding and decoding sqids.
Filters
{# Encode a single ID #} {{ user.id | sqids_encode }} {# Decode a sqid #} {{ sqid | sqids_decode | first }}
Functions
{# Encode multiple IDs #} {{ sqids_encode(1, 2, 3) }} {# Use in a path #} <a href="{{ path('user_show', { id: user.id | sqids_encode }) }}"> View User </a>
License
This bundle is released under the MIT License. See the LICENSE file for details.
roukmoute/sqids-bundle 适用场景与选型建议
roukmoute/sqids-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 08 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 roukmoute/sqids-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 roukmoute/sqids-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 46
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-02