0712-2888027 189-8648-0214
微信公眾號(hào)

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

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

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

時(shí)間:2019-10-20來(lái)源:風(fēng)信官網(wǎng) 點(diǎn)擊: 1978次
在項(xiàng)目開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)遇到需要唯一標(biāo)識(shí)碼的功能,風(fēng)信建站laravel開(kāi)發(fā)項(xiàng)目使用ramsey/uuid包進(jìn)行唯一識(shí)別碼的應(yīng)用,分享經(jīng)驗(yàn)如下:
 
github項(xiàng)目地址:https://github.com/ramsey/uuid
 
安裝軟件包:
 
composer require ramsey/uuid
 
安裝過(guò)程如下:
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
 
完成之后,我們?cè)陧?xiàng)目的控制器中進(jìn)行應(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";

}

熱門(mén)關(guān)鍵詞: laravel ramsey/uuid 唯一識(shí)別碼

您可能感興趣的相關(guān)文章:

欄目列表
推薦內(nèi)容
熱點(diǎn)內(nèi)容
展開(kāi)