lokhman / silex-hashids
Hashids库的Silex 2.0+服务提供者
2.0.1
2017-03-06 09:20 UTC
Requires
- hashids/hashids: ^2.0
- silex/silex: ~2.0
This package is auto-updated.
Last update: 2024-09-16 21:17:03 UTC
README
Hashids 服务提供者,用于 Silex 2.0+ 微型框架。
该项目是
silex-tools
库的一部分。
安装
您可以使用 Composer 安装 silex-hashids
composer require lokhman/silex-hashids
文档
从 Provider
命名空间启用 HashidsServiceProvider
以支持出色的 Hashids库。您可以为您的应用程序设置单个或多个配置文件。
use Lokhman\Silex\Provider\HashidsServiceProvider;
$app->register(new HashidsServiceProvider(), [
// default options for all profiles
'hashids.options' => [
'salt' => '',
'min_length' => 0,
'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
],
// optionally set multiple profiles
'hashids.profiles' => [
'profile1' => [
'min_length' => 8,
],
'profile2' => [
'salt' => 'this is my salt',
'alphabet' => 'abcdefghijklmnopqrstuvwxyz',
],
],
]);
// single profile encoding
$hash = $app['hashids']->encode(1, 2, 3);
$app['hashids']->encode(1, 2, 3) === $hash;
$app->hashids([1, 2, 3]) === $hash;
// single profile decoding
$id = $app['hashids']->decode($hash);
$app['hashids']->decode($hash) === $id;
$app->hashids($hash) === $id;
// multiple profiles encoding
$hash = $app['hashids']['profile1']->encode(1, 2, 3);
$app['hashids:profile1']->encode(1, 2, 3) === $hash;
$app->hashids([1, 2, 3], 'profile1') === $hash;
// multiple profiles decoding
$id = $app['hashids']['profile1']->decode($hash);
$app['hashids:profile1']->decode($hash) === $id;
$app->hashids($hash, 'profile1') === $id;
注意:要使用 hashids()
应用程序方法,将 HashidsTrait
包含到您的 Application
类中。该方法在出错时返回 FALSE
,如果给定的哈希包含单个编码的 id
,则返回整数,否则返回数组。
此外,服务提供者还支持自动转换带有 __hashids_
前缀的路由中的哈希参数。
// GET /users/jR (single profile)
$app->get('/users/{__hashids_id}', function(Application $app, $id) {
return $app->json(['id' => $id]); // { "id": 1 }
});
// GET /users/olejRejN (multiple profiles)
$app->get('/users/{__hashids_profile1_id}', function(Application $app, $id) {
return $app->json(['id' => $id]); // { "id": 1 }
});
许可证
库在MIT许可证下可用。包含的LICENSE文件详细说明了这一点。