danphyxius / hashids
Laravel 的 Hashids 扩展包
v1.3
2014-09-10 23:19 UTC
Requires
- php: >=5.4.0
- hashids/hashids: 0.3.1
- illuminate/support: ~4.0
This package is not auto-updated.
Last update: 2024-09-24 07:22:09 UTC
README
本包使用了由 hashids.org 创建的类
从数字生成哈希,如 YouTube 或 Bitly。当您不希望将数据库 ID 展示给用户时,请使用 Hashids。
安装
首先通过 Composer 安装该包。编辑您项目的 composer.json 文件以需要 ludo237/hashids。
"require": { "danphyxius/hashids": "1.3" }
接下来使用 Composer 从终端更新您的项目
php composer.phar update
安装包后,您需要添加服务提供者。打开您的 app/config/app.php 配置文件,并将新项目添加到 providers 数组中。
'DanPhyxius\Hashids\HashidsServiceProvider'
完成此操作后,您还需要添加一个别名。在您的 app/config/app.php 文件中,将其添加到 aliases 数组。
'Hashids' => 'DanPhyxius\Hashids\Hashids'
最后但同样重要的是,您需要从终端发布包配置
php artisan config:publish danphyxius/hashids
用法
完成所有步骤并完成安装后,您可以使用 Hashids。
加密
您可以简单地加密一个 ID
Hashids::encrypt(1); // Creating hash... Ri7Bi
或多个...
Hashids::encrypt(1, 21, 12, 12, 666); // Creating hash... MMtaUpSGhdA
解密
与加密相反
Hashids::decrypt('Ri7Bi'); // Returns array (size=1) 0 => int 1
或多个...
Hashids::decrypt('MMtaUpSGhdA'); // Returns array (size=5) 0 => int 1 1 => int 21 2 => int 12 3 => int 12 4 => int 666
注入 Hashids
现在还可以将 Hashids 注入到类中。以下是一个控制器的示例...
class ExampleController extends BaseController { protected $hashids; public function __construct(Hashids\Hashids $hashids) { $this->hashids = $hashids; } public function getIndex() { $hash = $this->hashids->encrypt(1); return View::make('example.index', compact('hash')); } }
原始的类名和命名空间已绑定到 IoC 容器中,以返回我们的 Hashids 实例化类。
使用 IoC
使用 IoC 创建一个 Hashids 实例
App::make('Hashids\Hashids')->encrypt(1);
这就完成了!
关于 Hashids 本身的文档,请访问:https://github.com/ivanakimov/hashids.php。
希望您喜欢这个包,并感谢 Ivan Akimov (@ivanakimov) 为制作 Hashids 所做的工作。Hashids 包的所有荣誉都归他所有。