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

孝感風(fēng)信網(wǎng)絡(luò)科技有限公司微信公眾號

當(dāng)前位置:主頁 > 技術(shù)支持 > PHP > laravel ramsey/uuid唯一識別碼應(yīng)用實戰(zhàn)

laravel ramsey/uuid唯一識別碼應(yīng)用實戰(zhàn)

時間:2019-10-20來源:風(fēng)信官網(wǎng) 點擊: 1978次
在項目開發(fā)過程中經(jīng)常會遇到需要唯一標(biāo)識碼的功能,風(fēng)信建站laravel開發(fā)項目使用ramsey/uuid包進行唯一識別碼的應(yīng)用,分享經(jīng)驗如下:
 
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
 
完成之后,我們在項目的控制器中進行應(yīng)用
 
use Ramsey\Uuid\Uuid;
 
$data = Uuid::uuid4();
$appid = $data->getHex();
dd($appid);
 
獲得結(jié)果如下:
 
"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";

}

熱門關(guān)鍵詞: laravel ramsey/uuid 唯一識別碼
欄目列表
推薦內(nèi)容
熱點內(nèi)容
展開