amamarul/integer-hashids 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

amamarul/integer-hashids

最新稳定版本:1.0.1

Composer 安装命令:

composer require amamarul/integer-hashids

包简介

Laravel Integers Hashids Generator. Encode and decode ids to Integers. Also generate prefixed hashids

README 文档

README

Make Integers Hashids

This package is an adaptation and combination of the following 3 packages:

This package works like Laravel Hashids but you can make integers hashids placing in alphabet connections only numbers and you can implement prefix ids. Also you can use encode() and decode() helpers. You can also make alphanumeric Hashids

Installation

Composer require

$ composer require amamarul/integer-hashids

Add Provider into config/app.php

Amamarul\Hashids\HashidsServiceProvider::class,

Publish config file

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

Usage

  1. Setup the Config file (config/hashids.php) You can create differents connections with differents parameters
    'default' => 'main',
    'prefix-separator' => '-',

    '<Name Connection>' => [
        'salt' => 'your-salt-string',
        'length' => '10',
        'alphabet' => '0123456789',
        'prefix' => null,
    ],
  • Name Connection: There are a 'main' connection that is used by default but you can create custom connections and then call them.

  • salt: is a phrase string.

  • length: Number of characters you need

  • alphabet: you can set any character to make the hash, but if you want integer hashid keep the same ('0123456789').

  • prefix: if you want a prefixed hashid you can add the prefix, if not you can omit it or delete the parameter.

  • prefix-separator: If you use a prefix maybe do you want to use a prefix separator. If not leave empty ('').

  • default: you can change the default connection

  1. With 'main' connection and without 'prefix' ('prefix' => null)
  use Hashids;

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

  Hashids::decode('7988887798');
  // output: 1548
  1. With 'main' connection and with 'prefix' ('prefix' => 'AA')
  use Hashids;

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

  Hashids::decode('AA-7988887798');
  // output: 1548
  1. With 'custom' name connection and with and without 'prefix'
  use Hashids;

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

  Hashids::connection('custom')->decode('7988887798');
  // output: 1548
  1. If you prefer to use dependency injection over facades, you can inject the manager:
  • With 'main' connection
      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)
      	}
      }
  • With 'custom' connection
      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)
      	}
      }

Helpers

You can use the 'encode()' and 'decode()' helpers

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

Feel free to send improvements

Created by Maru Amallo-amamarul

统计信息

  • 总下载量: 20.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-22