Laravel 使用 ramsey-uuid 生成UUID


想隨機一個唯一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

        不同的場景,可能還需安裝不同的依賴包!
 
    作者在文檔中提到了一個 '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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章