PHP code example of lingxi / hashids

1. Go to this page and download the library: Download lingxi/hashids library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lingxi / hashids example snippets


Lingxi\Hashids\HashidsServiceProvider::class

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



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',
        ],

    ],

];


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



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');
    }
}

Post::first()->public_id
bash
php artisan vendor:publish --provider='Lingxi\Hashids\HashidsServiceProvider'