定制 novay/laravel-uuid 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

novay/laravel-uuid

Composer 安装命令:

composer require novay/laravel-uuid

包简介

Laravel package to generate and to validate a UUID according to the RFC 4122 standard. UUID Versions 1, 3, 4 and 5 are supported.

关键字:

README 文档

README

Total Downloads Build Status Latest Stable Version Latest Unstable Version License

Laravel package to generate a UUID according to the RFC 4122 standard. UUID Versions 1, 3, 4 and 5 are supported. With MIT license.

About

Since Laravel 4.* and 5.* both rely on either OpenSSL or Mcrypt, the pseudo random byte generator now tries to use one of them. If both cannot be used (not a Laravel project?), the 'less random' mt_rand() function is used.

Requirements

Installation

Laravel 5.5 and above
  1. From your projects root folder in terminal run:
    composer require novay/laravel-uuid
  • Uses package auto discovery feature, no need to edit the config/app.php file.
Laravel 5.4 and below
  1. From your projects root folder in terminal run:
    composer require novay/laravel-uuid:2.0
  1. Register the package with laravel in config/app.php under aliases with the following:
    'aliases' => [
        'Uuid' => Novay\Uuid\Uuid::class,
    ];

Basic Usage

To quickly generate a UUID just do

    Uuid::generate()
  • This will generate a version 1 with a random ganerated MAC address.

To echo out the generated Uuid cast it to a string

(string) Uuid::generate()

or

Uuid::generate()->string

Advanced Usage

UUID creation

UUID V1

Generate a version 1, time-based, UUID. You can set the optional node to the MAC address. If not supplied it will generate a random MAC address.

Uuid::generate(1,'00:11:22:33:44:55');
UUID V3

Generate a version 3, name-based using MD5 hashing, UUID

Uuid::generate(3,'test', Uuid::NS_DNS);
UUID V4

Generate a version 4, truly random, UUID

Uuid::generate(4);
UUID V5

Generate a version 5, name-based using SHA-1 hashing, UUID

Uuid::generate(5,'test', Uuid::NS_DNS);

Additional Features

Import UUID
  • To import a UUID
$uuid = Uuid::import('d3d29d70-1d25-11e3-8591-034165a3a613');
Extract Time
  • Extract the time for a time-based UUID (version 1)
$uuid = Uuid::generate(1);
dd($uuid->time);
Extract Version
  • Extract the version of an UUID
$uuid = Uuid::generate(4);
dd($uuid->version);
Eloquent UUID Generation

If you want an UUID magically be generated in your Laravel models, just add this boot function to your Model.

/**
 *  Setup model event hooks
 */
public static function boot()
{
    parent::boot();
    self::creating(function ($model) {
        $model->uuid = (string) Uuid::generate(4);
    });
}

This will generate a version 4 UUID when creating a new record.

Model Binding to UUID instead of Primary Key

If you want to use the UUID in URLs instead of the primary key, you can add this to your model (where 'uuid' is the column name to store the UUID)

/**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'uuid';
}

When you inject the model on your resource controller methods you get the correct record

public function edit(Model $model)
{
   return view('someview.edit')->with([
        'model' => $model,
    ]);
}
Validation

Just use like any other Laravel validator.

'uuid-field' => 'uuid'

Or create a validator from scratch. In the example an Uuid object in validated. You can also validate strings $uuid->string, the URN $uuid->urn or the binary value $uuid->bytes

$uuid = Uuid::generate();
$validator = Validator::make(['uuid' => $uuid], ['uuid' => 'uuid']);
dd($validator->passes());

Credits

  • Full development credit must go to webpatser. This package was forked and modified to be compliant with MIT licensing standards for production use.

Notes

Full details on the UUID specification can be found here

License

Laravel UUID is licensed under the MIT license for both personal and commercial products. Enjoy!

统计信息

  • 总下载量: 103
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固