今天项目中,想随机一个唯一id,想到php的 uniqid(),或者是其他方式生成一个唯一id。
这种场景其实是很多的,但是也想不起之前是怎么写的,哪里写的,就看看laravel有没有依赖包,上网一搜,发现了这个:
ramsey/uuid
github地址:
https://github.com/ramsey/uuid
官网地址:
https://benramsey.com/projects/ramsey-uuid/
看来,作者应该有不少项目,没看,记录下,有时间了看!
搜索一下,就大概了解了些,这里简单介绍下:
ramsey/uuid,支持PHP5.4+,用于生成RFC 4122描述的 1,3,4,5 这几类的UUID(应该是这个,没看。。。)
安装:
composer require ramsey/uuid
我这边安装完后,提示:
ramsey/uuid suggests installing ircmaxell/random-lib (Provides RandomLib for use with the RandomLibAdapter)
ramsey/uuid suggests installing ext-libsodium (Provides the PECL libsodium extension for use with the SodiumRandomGenerator)
ramsey/uuid suggests installing ext-uuid (Provides the PECL UUID extension for use with the PeclUuidTimeGenerator and PeclUuidRandomGenerator)
ramsey/uuid suggests installing moontoast/math (Provides support for converting UUID to 128-bit integer (in string form).)
ramsey/uuid suggests installing ramsey/uuid-doctrine (Allows the use of Ramsey\Uuid\Uuid as Doctrine field type.)
ramsey/uuid suggests installing ramsey/uuid-console (A console application for generating UUIDs with ramsey/uuid)
不同的场景,可能还需安装不同的依赖包!
作者在文档中提到了一个 'API文档',ramsey/uuid,使用了 'ApiGen' 来生成API文档(作者又给我们介绍了一个PHP依赖包,看我们以后会不会用上)
https://github.com/ApiGen/ApiGen
使用方法:
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";
}
分享2篇文章:
http://www.mamicode.com/info-detail-1576539.html
http://www.jb51.net/article/75035.htm
有点应付啊!有时间了再细看文档、源码,再总结!先知道有这个就行!
PHP 使用 ramsey-uuid 生成UUID笔记
最新推荐文章于 2025-03-29 01:31:38 发布