a-sabagh/laravel-sqids
Composer 安装命令:
composer require a-sabagh/laravel-sqids
包简介
Laravel integration for sqids-php: Generate short YouTube-looking IDs from numbers
README 文档
README
Laravel adapter for sqids-php.
Generate short, unique, non-sequential IDs for your models, routes, validation, and more.
Table of Contents
Features
- Generate short, unique, non-sequential IDs
- Easy helpers:
sqid($id)/unsqid($hash) - Facade:
Sqids::encode()/decode() - Validation rule:
sqid - Fully configurable alphabet, min length, and blocklist
Installation
composer require a-sabagh/laravel-sqids
Simple Encode & Decode
The Sqids service allows you to convert integers into short, URL-safe strings and decode them back.
Important
Sqids require either the bcmath or gmp extension in order to work.
Usage
use ASabagh\LaravelSqids\Facades\Sqids; $id = 123; $encodeString = Sqids::encode([$id]); // e.g. "Lqj8a0" $decodeNumber = Sqids::decode($encodeString); // [123]
Encode and Decode Single Integer
$id = 456; $encodeString = Sqids::encodeInteger($id); $decodeInteger = Sqids::decodeInteger($encodeString);
Decode and get a Laravel Collection
$decodeCollection = Sqids::decodeCollect($encodeString);
Notes:
-
encodealways expects an array of integers. -
decodereturns an array. -
encodeInteger/decodeIntegerwork with single integers. -
decodeCollectreturns aCollectionfor easier chaining with Laravel collections.
Configuration
Laravel Sqids allows you to customize its behavior via a configuration file. Publishing the configuration lets you modify settings such as the alphabet, minimum length, and blocklist.
Publish the Config File
Use the following Artisan command to publish the configuration to your application:
php artisan vendor:publish --tag=sqids
Here are each of the drivers setup for your application. Example configuration has been included, but you may add as many drivers as you would like.
'drivers' => [ 'default' => [ 'pad' => env('SQIDS_DEFAULT_PAD', ''), 'length' => env('SQIDS_DEFAULT_LENGTH', 6), 'blocklist' => env('SQIDS_DEFAULT_BLOCK_LIST', []), 'alphabet' => env('SQIDS_DEFAULT_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), ], ]
Laravel Sqids allows you to publish the configuration and define multiple drivers, each with its own settings.
Driver Setup
The configuration file includes a drivers array. You can configure one or more drivers for different encoding strategies.
You can add additional drivers with their own configuration:
'drivers' => [ 'default' => [ /* default config */ ], 'short_ids' => [ 'pad' => '0', 'length' => 4, 'blocklist' => ['0', 'O', 'I', '1'], 'alphabet' => 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789', ], ],
Then, you can use a specific driver when encoding/decoding:
$encoded = Sqids::driver('short_ids')->encode([$id]); $decoded = Sqids::driver('short_ids')->decode($encoded);
Once a driver has been registered and configured in your config/sqids.php, you can access it directly using a camelCase method on the Sqids Facade.
$shortId = Sqids::shortIds()->encode([$id]); $defaultId = Sqids::default()->encode([$id]);
Check if an Encoded String is Valid
$isValid = Sqids::encodedStringValid($randomString);
Sqids Custom Validation Rule
Laravel Sqids provides a custom validation rule to ensure that a given input is a valid Sqid string. You can use it in form requests, inline validation, and with custom drivers.
$encoded = Sqids::encodeInteger($id); $validator = Validator::make( ['endpoint' => $encoded], ['endpoint' => [new SqidsValidationRule]] ); if ($validator->passes()) { // Valid Sqid }
Available Helpers
| Function |
|---|
sqids(array $numbers, ?string $driver = null): string |
unsqids(string $encodedString, ?string $driver = null): array |
sqidsInt(int $id, ?string $driver = null): int |
unsqidsInt(string $encodedString, ?string $driver = null): int |
unsqidsCollect(string $encodedString, ?string $driver = null): Collection |
// Encode multiple numbers $encoded = sqids([1, 2, 3]); // e.g. "Lqj8a0" // Decode back $decoded = unsqids($encoded); // [1, 2, 3] // Encode a single integer $singleEncoded = sqidsInt(123); // e.g. "M7k2b1" // Decode single integer $singleDecoded = unsqidsInt($singleEncoded); // 123 // Decode into a Collection $collection = unsqidsCollect($encoded); // Collection([1, 2, 3])
a-sabagh/laravel-sqids 适用场景与选型建议
a-sabagh/laravel-sqids 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 213 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「ids」 「hashids」 「sqids」 「short-quick-ids」 「short-quality-ids」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 a-sabagh/laravel-sqids 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 a-sabagh/laravel-sqids 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 a-sabagh/laravel-sqids 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dynamically create a hash of an Eloquent model id value to avoid exposing a record's actual database id.
Hashids for Yii2
A collection of observer classes that can be use in your Laravel application.
Backend Optimizing Bundle
Provides hash tools for Laravel.
Webman Hashids Used to generate a youtube-like ID from a digital ID
统计信息
- 总下载量: 213
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-21