amamarul/integer-hashids

Laravel 整数 Hashids 生成器。将 ID 编码和解码为整数。还可以生成带前缀的 hashids

安装量: 18,759

依赖项: 1

建议者: 0

安全: 0

星标: 4

关注者: 2

分支: 5

开放问题: 3

类型:软件包

1.0.1 2018-01-05 08:49 UTC

This package is not auto-updated.

Last update: 2024-09-19 14:24:10 UTC


README

创建整数 Hashids

本软件包是以下 3 个软件包的改编和组合

本软件包的功能类似于 Laravel Hashids,但您可以将整数 hashids 放置在只有数字的字母连接中,并且可以实施前缀 ID。 您还可以使用 encode() 和 decode() 辅助函数。 您还可以创建字母数字 Hashids

安装

Composer 需要

$ composer require amamarul/integer-hashids

将提供程序添加到 config/app.php 配置文件中

Amamarul\Hashids\HashidsServiceProvider::class,

发布配置文件

$ php artisan vendor:publish --provider='Amamarul\Hashids\HashidsServiceProvider'

使用方法

  1. 设置配置文件(《config/hashids.php》)您可以使用不同的参数创建不同的连接
    'default' => 'main',
    'prefix-separator' => '-',

    '<Name Connection>' => [
        'salt' => 'your-salt-string',
        'length' => '10',
        'alphabet' => '0123456789',
        'prefix' => null,
    ],
  • 连接名称:存在一个默认使用的'main'连接,但您也可以创建自定义连接,然后调用它们。

  • salt: 是一个短语字符串。

  • length: 您需要的字符数

  • alphabet: 您可以设置任何字符来生成哈希,但如果是整数 hashid,请保持相同('0123456789')。

  • prefix: 如果您想要带前缀的 hashid,您可以添加前缀,如果不想要,可以省略或不删除参数。

  • prefix-separator: 如果您使用前缀,可能还想使用前缀分隔符。如果不使用,请留空('')。

  • default: 您可以更改默认连接

  1. 使用 'main' 连接且无 'prefix'(《'prefix' => null》)
  use Hashids;

  Hashids::encode(1548);
  // output: 7988887798

  Hashids::decode('7988887798');
  // output: 1548
  1. 使用 'main' 连接且有 'prefix'(《'prefix' => 'AA'》)
  use Hashids;

  Hashids::encode(1548);
  // output: AA-7988887798

  Hashids::decode('AA-7988887798');
  // output: 1548
  1. 使用 'custom' 名称连接,有或无 'prefix'
  use Hashids;

  Hashids::connection('custom')->encode(1548);
  // output: 7988887798

  Hashids::connection('custom')->decode('7988887798');
  // output: 1548
  1. 如果您更喜欢使用依赖注入而不是外观,则可以注入管理器
  • 使用 'main' 连接
      use Amamarul\Hashids\Support\HashidsManager;
    
      class Foo
      {
      	protected $hashids;
    
      	public function __construct(HashidsManager $hashids)
      	{
      		$this->hashids = $hashids;
      	}
    
      	public function encode($id)
      	{
      		$this->hashids->encode($id)
      	}
    
      	public function decode($hashid)
      	{
      		$this->hashids->decode($hashid)
      	}
      }
  • 使用 'custom' 连接
      use Amamarul\Hashids\Support\HashidsManager;
    
      class Foo
      {
      	protected $hashids;
    
      	public function __construct(HashidsManager $hashids)
      	{
      		$this->hashids = $hashids->connection('custom');
      	}
    
      	public function encode($id)
      	{
      		$this->hashids->encode($id)
      	}
    
      	public function decode($hashid)
      	{
      		$this->hashids->decode($hashid)
      	}
      }

辅助函数

您可以使用 'encode()' 和 'decode()' 辅助函数

  • encode()
  // with 'main' connection
  encode($id)
  // with 'custom' connection
  encode($id,'custom')
  • decode()
  // with 'main' connection
  decode($hashid)
  // with 'custom' connection
  decode($hashid,'custom')

欢迎提出改进意见

Maru Amallo-amamarul 创建