damess / zf2-hashids
Zend Framework 2 模块,允许您在 API 中混淆 ID
v0.1.1
2015-09-10 13:16 UTC
Requires
- php: >=5.3.3
- hashids/hashids: 1.0.*
- zendframework/zend-mvc: 2.*
- zendframework/zend-servicemanager: 2.*
- zendframework/zend-stdlib: 2.*
Requires (Dev)
- phpunit/phpunit: 4.*
- satooshi/php-coveralls: ~0.6
- squizlabs/php_codesniffer: 2.3.*
- zendframework/zendframework: 2.*
This package is not auto-updated.
Last update: 2024-09-10 03:39:17 UTC
README
完整文档
这是一个用于 PHP Hashids 库 的 Zend Framework 2 模块。请参阅库的完整文档,地址为 http://hashids.org/php。
需求
- PHP 5.3.3 或更高版本
- Zend Framework 2.1 及以上
(可选) GNU 多精度或 BCMath 以允许编码大于 1,000,000,000 的整数。更多信息请参阅 https://github.com/ivanakimov/hashids.php。
使用 Composer 安装
composer require damess/zf2-hashids
将模块添加到 ./config/application.config.php
<?php return array( 'modules' => array( 'Application', 'DaMess\Hashids', ), ... );
选项
Hashids 模块提供一些选项,允许您快速更改配置。安装模块后,将 ./vendor/damess/zf2-hashids/config/hashids.global.php.dist 复制到 ./config/autoload/hashids.global.php,并按需更改值。
- 盐值 - 默认值为 ''. 这是将 ID 编码为散列时使用的值。注意:请勿更改已设置的值。
- 最小长度 - 默认值为 22。这定义了编码散列值的最低长度。
- 字母表 - 默认值为 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'。这是编码 ID 时使用的字母表。此字母表必须至少 16 个字符长,且不得包含任何空格。
<?php return array( 'hashids' => array( /* * The salt to use for encryption * 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。
在实现 Zend\ServiceManager\FactoryInterface 的工厂中
<?php $service = $serviceLocator->get('DaMess\Hashids\Service\HashidsService'); $service->encode(1); // 39J4q2VolejRejNmGQBW71 (assuming default config values) $service->decode('39J4q2VolejRejNmGQBW71'); // array(1) (assuming default config values)
在实现 Zend\ServiceManager\ServiceLocatorAwareInterface 并从 ServiceManager 中获取的控制器或类中
<?php $service = $this->getServiceLocator()->get('DaMess\Hashids\Service\HashidsService'); $service->encode(1); // 39J4q2VolejRejNmGQBW71 (assuming default config values) $service->decode('39J4q2VolejRejNmGQBW71'); // array(1) (assuming default config values)
控制器插件
Hashids 模块提供 hashids 控制器插件。以下是使用方法:
<?php namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class IndexController extends AbstractActionController { public function indexAction() { return new ViewModel(array( 'hash' => $this->hashids()->encode(1), // 39J4q2VolejRejNmGQBW71 'id' => $this->hashids()->decode('39J4q2VolejRejNmGQBW71'), // array(1) )); } }