torann / hashids
Laravel 对 Hashids 的扩展包
3.0.5
2024-03-22 18:47 UTC
Requires
- php: ^7.4|^8.0
- hashids/hashids: ~4.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- mockery/mockery: ^1.3
- phpstan/phpstan: ^0.12.14
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.5
README
此包使用了由 hashids.org 创建的类
从数字生成哈希,例如 YouTube 或 Bitly。当您不希望向用户暴露数据库 ID 时,请使用 hashids。
安装
Composer
从命令行运行以下命令
$ composer require torann/hashids
无需包自动发现
一旦 Hashids 安装完毕,您需要将服务提供者和外观注册到应用程序中。打开 config/app.php
并找到 providers
和 aliases
键。
'providers' => [ Torann\Hashids\HashidsServiceProvider::class, ] 'aliases' => [ 'Hashids' => Torann\Hashids\Facade\Hashids::class, ]
发布配置
在项目的根目录下从命令行运行此命令
$ php artisan vendor:publish --provider="Torann\Hashids\HashidsServiceProvider"
配置文件将被发布到 config/hashids.php
。
使用方法
遵循所有步骤并完成安装后,您可以使用 Hashids。
编码
您可以简单地编码单个 ID
Hashids::encode(1); // Returns Ri7Bi
或多个...
Hashids::encode(1, 21, 12, 12, 666); // Returns MMtaUpSGhdA
解码
Hashids::decode(Ri7Bi); // Returns array (size=1) 0 => int 1
或多个...
Hashids::decode(MMtaUpSGhdA); // Returns array (size=5) 0 => int 1 1 => int 21 2 => int 12 3 => int 12 4 => int 666
Hashids 的所有功劳都属于 Ivan Akimov (@ivanakimov),感谢他创建了这个工具!