lingxi/hashids

A Hashids bridge for Laravel

v0.1.1 2018-11-08 11:01 UTC

This package is not auto-updated.

Last update: 2024-11-04 07:27:19 UTC


README

Latest Stable VersionTotal DownloadsLicense

安装

composer require lingxi/hashids

添加到 app.php

Lingxi\Hashids\HashidsServiceProvider::class
'Hashids' => Lingxi\Hashids\Facades\Hashids::class

Config

php artisan vendor:publish --provider='Lingxi\Hashids\HashidsServiceProvider'
<?php

return [

    'default' => 'main',

    'middleware' => [
        'open' => true,

        // 路由中需要被 decode 的 id
        'route_parameters' => [
            //
        ],

        // 请求参数需要被 decode 的 id
        'request_parameters' => [
            //
        ]
    ],

    // 开启严格模式之后,解密 id 错误会抛出异常
    'strict' => [
        'enable' => true,
        'default' => 0,
    ],

    'connections' => [

        'main' => [
            'prefix' => '',
            'salt' => 'your-salt-string',
            'length' => 'your-length-integer',
            'alphabet' => 'your-alphabet-string',
        ],

    ],

];

Middleware

添加中间件

如果是全局中间件,那么对 route 参数是无法自动 decode 的

\Lingxi\Hashids\Middleware\DecodePublicIdMiddleware::class,

在 config/hashids.php 中去配置需要解密的路由参数以及请求参数

Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Lingxi\Hashids\ModelTraits\PublicId;

class Post extends Model
{
    use PublicId;

    public function comments()
    {
        return $this->morphMany(\App\Comment::class, 'commentable');
    }
}

Then, get public id.

Post::first()->public_id

Debug

php artisan id:en 1 --uri=p

php artisan id:de sdfghjkla;sdjhasjhdfgahjsdjasd