PHP code example of yexiaofeng / hashid

1. Go to this page and download the library: Download yexiaofeng/hashid 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/ */

    

yexiaofeng / hashid example snippets


echo str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');


echo id_encode(4568); //输出:N5lkv0
  
echo id_decode('N5lkvO'); //输出:4568
  
//不可对float类型数字加密,不可对负数加密,给定任何非正整数参数都会抛出错误,如:
echo id_encode(2.36); //非正整数,抛出错误
echo id_encode(-23); //非正整数,抛出错误
  
//解密时任何无效字符串参数或校验错误都会抛出错误, 如:
echo id_decode('m_Dl9'); //包含无效字符,抛出错误
echo id_decode('nlK8GhRW'); //校验错误,抛出错误



namespace App;
  
use Illuminate\Database\Eloquent\Model;
use Yexiaofeng\Hashid\Traits\Hashid;
 
class User extends Model
{
    use Hashid;

}

public function getPidAttribute($value)
{
  return id_encode($value);
}

'hashid' => \Yexiaofeng\Hashid\Http\Middleware\Hashid::class,

Route::resource('/users', 'UserController')->middleware('hashid');

Route::get('users/{user}/posts/{post}/comments/{comment}', function ($user, $post, $comment) {
    //
})->middleware('hashid:user,post');