0712-2888027 189-8648-0214
微信公众号

孝感风信网络科技有限公司微信公众号

当前位置: 主页 > 技术支持 > PHP > laravel ramsey/uuid唯一识别码应用实战

laravel ramsey/uuid唯一识别码应用实战

时间:2019-10-20来源:风信网 点击:
在项目开发过程中经常会遇到需要唯一标识码的功能,风信建站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 Hashids一款不错的哈希数据ID解

    在Laravel中我们使用composer进行安装 vinkla/hashids开源库,可以将字...

  • 一个经典的PHP文件上传类fileupload.c

    一、需求分析 要球自定义文件上传类,即在使用非常简便的前提...

  • Laravel Controller控制器传值到View视图的

    总体内容分为传值类型和方法,大概总体感觉如下。 传值类型...

  • 解决Laravel Warning: require(): open_basedi

    新项目布置Laravel,所有源代码数据上传至服务器中,配置好运行...

  • PHP下封杀野蜘蛛Spider/Bot的方法

    最近看了亚搏体育平台官方的访问日志,发现几个网络蜘蛛吃了大部分的流量...

  • Destoon进行二次开发需要的准备工作

    能够理很好理解Destoon的构架的原理 扎实的PHP基础,熟悉结构化...

  • laravel扩展包renatomarinho/laravel-page-sp

    压缩 HTML 加速网页响应 renatomarinho/laravel-page-speed,扩展包可以自...

  • 使用laravel agent轻松识别客户端信息以

    有很多时候我们需要在laravel中识别系统是安卓还是ios,有时候我...

  • 热点内容
    展开