Create unique GUID with PHP
In this blog post, I am going to write about my little PHP package for creating globally unique identifiers (GUID).
Begin with installation via Composer:
composer require sudiptpa/guid
I was inspired mainly to create this package while I was searching for GUID information in the php.net official PHP website. I found a good solution by a community member Dave Pearson, view source code here.
I was looking a solution for Laravel, so I created this package as reusable for everyone as an opensource package.
Usage
In order to consume this package from Laravel application, register the package service provider within your config/app.php
file.
'providers' => [
Sujip\Guid\GuidServiceProvider::class,
]
'aliases' => [
'Guid' => 'Sujip\Guid\Guid'
]
If your Laravel application is running with v5.5
and higher version, this package also has support for package auto-discovery, Laravel will automatically register its service providers and facades when it is installed, creating a convenient installation experience for you.
If you are a Laravel Developer:
Make use of Laravel Facade:
echo "GUID: " . Guid::create(); //example output : 2b23924f-0eaa-4133-848e-7ce1edeca8c9
or use a helper function guid()
echo "GUID: " . guid(); // example output: 2b23924f-0eaa-4133-848e-7ce1edeca8c9
If you want to use this package outside of the framework.
use Sujip\Guid\Guid;
$guid = new Guid;
$guid = $guid->create();
Output
//Example: 2b23924f-0eaa-4133-848e-7ce1edeca8c9
Conclusion
This package is framework agnostic, which means it can be used in any type of PHP projects.
Thanks for reading up to the end. If you have any feedback regarding this blog post feel free to leave your comment below. Also don't forget to share with friends if you like it.
Happy Coding!