0712-2888027 189-8648-0214
微信公眾號

孝感風信網絡科技有限公司微信公眾號

當前位置:主頁 > 技術支持 > PHP > laravel ramsey/uuid唯一識別碼應用實戰

laravel ramsey/uuid唯一識別碼應用實戰

時間:2022-01-24來源:風信官網 點擊: 790次
在項目開發過程中經常會遇到需要唯一標識碼的功能,風信建站laravel開發項目使用ramsey/uuid包進行唯一識別碼的應用,分享經驗如下:
 
github項目地址:https://github.com/ramsey/uuid
 
安裝軟件包:
 
composer require ramsey/uuid
 
安裝過程如下:
Using version ^3.8 for ramsey/uuid
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: [32mbeyondcode/laravel-dump-server[39m
Discovered Package: [32mfideloper/proxy[39m
Discovered Package: [32mintervention/image[39m
Discovered Package: [32mjenssegers/agent[39m
Discovered Package: [32mlaravel/tinker[39m
Discovered Package: [32mmews/captcha[39m
Discovered Package: [32mnesbot/carbon[39m
Discovered Package: [32mnunomaduro/collision[39m
[32mPackage manifest generated successfully.[39m
 
完成之后,我們在項目的控制器中進行應用
 
use Ramsey\Uuid\Uuid;
 
$data = Uuid::uuid4();
$appid = $data->getHex();
dd($appid);
 
獲得結果如下:
 
"81b6c500e42d43d0a77a99bb7d3ab8a5"
 
官方示例代碼:
 

require 'vendor/autoload.php';

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;

try {

    // Generate a version 1 (time-based) UUID object
    $uuid1 = Uuid::uuid1();
    echo $uuid1--->toString() . "\n"; // i.e. e4eaaaf2-d142-11e1-b3e4-080027620cdd

    // Generate a version 3 (name-based and hashed with MD5) UUID object
    $uuid3 = Uuid::uuid3(Uuid::NAMESPACE_DNS, 'php.net');
    echo $uuid3->toString() . "\n"; // i.e. 11a38b9a-b3da-360f-9353-a5a725514269

    // Generate a version 4 (random) UUID object
    $uuid4 = Uuid::uuid4();
    echo $uuid4->toString() . "\n"; // i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a

    // Generate a version 5 (name-based and hashed with SHA1) UUID object
    $uuid5 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'php.net');
    echo $uuid5->toString() . "\n"; // i.e. c4a760a8-dbcf-5254-a0d9-6a4474bd1b62

} catch (UnsatisfiedDependencyException $e) {

    // Some dependency was not met. Either the method cannot be called on a
    // 32-bit system, or it can, but it relies on Moontoast\Math to be present.
    echo 'Caught exception: ' . $e->getMessage() . "\n";

}

熱門關鍵詞: laravel ramsey/uuid 唯一識別碼
欄目列表
推薦內容
熱點內容
展開
亚洲乱亚洲乱妇无码