elfsundae/laravel-hashid
Composer 安装命令:
composer require elfsundae/laravel-hashid
包简介
A simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.
README 文档
README
Laravel Hashid provides a unified API across various drivers such as Base62, Base64, Hashids and Optimus, with support for multiple connections or different encoding options. It offers a simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.
Installation
You can install this package using the Composer manager:
$ composer require elfsundae/laravel-hashid
For Lumen or earlier Laravel than v5.5, you need to register the service provider manually:
ElfSundae\Laravel\Hashid\HashidServiceProvider::class
Then publish the configuration file:
# For Laravel application: $ php artisan vendor:publish --tag=hashid # For Lumen application: $ cp vendor/elfsundae/laravel-hashid/config/hashid.php config/hashid.php
Configuration
Our well documented configuration file is extremely similar to the configurations of numerous Laravel manager integrations such as Database, Queue, Cache and Filesystem. So you do not need to spend extra time to learn how to configure Hashid.
Additionally, for simplicity you do not need to add singleton drivers like Base64 to your config file as they have no encoding options, unless you would like to specify a meaningful connection name.
Let's see an example of the configuration:
'default' => 'id', 'connections' => [ 'basic' => [ 'driver' => 'base64', ], 'hashids' => [ 'driver' => 'hashids', 'salt' => 'sweet girl', ], 'id' => [ 'driver' => 'hashids_integer', 'salt' => 'My Application', 'min_length' => 6, 'alphabet' => '1234567890abcdef', ], 'base62' => [ 'driver' => 'base62', 'characters' => 'f9FkqDbzmn0QRru7PBVeGl5pU28LgIvYwSydK41sCO3htaicjZoWAJNxH6EMTX', ], ],
Usage
The hashid() helper or the Hashid facade may be used to interact with any of your configured connections or drivers:
use ElfSundae\Laravel\Hashid\Facades\Hashid; // Obtain the default connection instance hashid(); Hashid::connection(); // Obtain the "base62" connection instance hashid('base62'); Hashid::connection('base62'); // Obtain the Base64 driver instance hashid('base64'); Hashid::connection('base64'); Hashid::driver('base64');
There are only two methods you need to know to use any connection or driver:
encode($data)for encoding data.decode($data)for decoding data.
hashid()->encode(123456); hashid('base64')->decode('TGFyYXZlbA'); Hashid::encode(123456); Hashid::connection('hashids')->decode('X68fkp');
And there are also two corresponding helper functions:
hashid_encode($data, $name = null)hashid_decode($data, $name = null)
hashid_encode(123456); hashid_decode('TGFyYXZlbA', 'base64');
Built-in Drivers
Base62
- Drivers:
base62,base62_integer - Configuration:
characters: 62 unique characters
- Backend:
tuupola/base62 - Notes:
- You may use the
hashid:alphabetcommand to generate random characters. - GMP is strongly recommended as it is much faster than pure PHP.
- You may use the
Base64
- Drivers:
base64,base64_integer - Backend:
elfsundae/urlsafe-base64
Hashids
- Drivers:
hashids,hashids_hex,hashids_integer,hashids_string - Configuration:
saltmin_lengthalphabet: At least 16 unique characters
- Backend:
hashids/hashids - Notes:
- You may use the
hashid:alphabetcommand to generate a random alphabet. - GMP is strongly recommended.
- You may use the
Hex
- Drivers:
hex,hex_integer
Optimus
- Drivers:
optimus - Configuration:
prime: Large prime number lower than2147483647inverse: The inverse prime so that(PRIME * INVERSE) & MAXID == 1random: A large random integer lower than2147483647
- Backend:
jenssegers/optimus - Notes:
- You may use the
hashid:optimuscommand to generate needed numbers. - Only for integer numbers.
- The max number can be handled correctly is
2147483647.
- You may use the
Custom Drivers
To create a custom Hashid driver, you only need to implement the ElfSundae\Laravel\Hashid\DriverInterface interface that contains two methods: encode and decode. The constructor can optionally receive the driver configuration from a $config argument, and type-hinted dependencies injection is supported as well:
<?php namespace App\Hashid; use ElfSundae\Laravel\Hashid\DriverInterface; use Illuminate\Contracts\Encryption\Encrypter; class CustomDriver implements DriverInterface { protected $encrypter; protected $serialize; public function __construct(Encrypter $encrypter, array $config = []) { $this->encrypter = $encrypter; $this->serialize = $config['serialize'] ?? false; } public function encode($data) { return $this->encrypter->encrypt($data, $this->serialize); } public function decode($data) { return $this->encrypter->decrypt($data, $this->serialize); } }
Now you can configure the connection with this driver:
'connections' => [ 'custom' => [ 'driver' => App\Hashid\CustomDriver::class, 'serialize' => false, ], // ... ]
If you prefer a short name for your driver, just register a container binding with hashid.driver. prefix:
$this->app->bind('hashid.driver.custom', CustomDriver::class);
Testing
$ composer test
License
This package is open-sourced software licensed under the MIT License.
elfsundae/laravel-hashid 适用场景与选型建议
elfsundae/laravel-hashid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 260.21k 次下载、GitHub Stars 达 412, 最近一次更新时间为 2017 年 10 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「base64」 「hashids」 「obfuscate」 「hashid」 「base62」 「optimus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 elfsundae/laravel-hashid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 elfsundae/laravel-hashid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 elfsundae/laravel-hashid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dynamically create a hash of an Eloquent model id value to avoid exposing a record's actual database id.
Hashids for Yii2
AMWSCAN (Antimalware Scanner) is a php antimalware/antivirus scanner console script written in php for scan your project. This can work on php projects and a lot of others platform.
A collection of observer classes that can be use in your Laravel application.
base64 image verification captcha library for webman plugin
Provides hash tools for Laravel.
统计信息
- 总下载量: 260.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 415
- 点击次数: 39
- 依赖项目数: 2
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-06