vuoriliikaluoma/zf3-hashids

此包已被废弃,不再维护。未建议替代包。

Zend Framework 3模块,允许您在API中混淆ID

2.0.0 2018-03-12 13:49 UTC

This package is auto-updated.

Last update: 2021-03-29 00:35:21 UTC


README

这是ZF2 Hashids的分支,已更新以适应ZF3。

我还添加了View Helper,并可以访问Hashids的encodeHexdecodeHex方法。

完整文档

这是PHP Hashids库的Zend Framework 3模块。

请参阅库的完整文档:http://hashids.org/php

需求

  • PHP 5.6或更高版本
  • PHP-GMP (GNU Multiple Precision)
  • Zend Framework 3

更多信息请参阅https://github.com/ivanakimov/hashids.php

使用Composer安装

composer require vuoriliikaluoma/zf3-hashids

将模块添加到./config/application.config.php

<?php

return [
    'modules' => [
        ...
        'Application',
        'DaMess\Hashids',
        ...
    ],
    ...
];

选项

Hashids模块提供了一些选项,以便您快速更改配置。安装模块后,将./vendor/vuoriliikaluoma/zf3-hashids/config/hashids.global.php.dist复制到./config/autoload/hashids.global.php,并按需更改值。

  • 盐值 - 默认值为''。这是将ID编码为哈希时使用的值。注意:请勿更改已设置的此值。
  • 最小长度 - 默认值为22。这定义了编码哈希值的最低长度。
  • 字母表 - 默认值为'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'。编码ID时使用的字母表。此字母表至少应包含16个字符,且不得包含空格。
<?php

return [
    'hashids' => [
        // The salt to use when encoding an id to a hash and decoding a hash to an id
        // NOTE: Do not change this once it's been set
        'salt' => '',
        // Minimum length of the generated hash
        'min_length' => 22,
        // Define which characters are used when building the hash
        'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
    ],
];

使用方法

此模块提供HashidsService和一个控制器插件,以下为使用示例。

HashidsService

可以从ServiceManager获取HashidsService。

<?php

use DaMess\Hashids\Service\HashidsService;

$service = $container->get(HashidsService::class);
$service->encode(1); // 39J4q2VolejRejNmGQBW71 (assuming default config values)
$service->decode('39J4q2VolejRejNmGQBW71'); // array(1) (assuming default config values)

在控制器类或视图文件中

ZF3 Hashids附带控制器插件和视图助手,因此可以从任何控制器方法或视图文件中使用。

<?php

$this->hashids()->encode(1); // 39J4q2VolejRejNmGQBW71 (assuming default config values)
$this->hashids()->decode('39J4q2VolejRejNmGQBW71'); // array(1) (assuming default config values)