fanmade / laravel-nanoid
A small package to add Nano IDs to Laravel
1.0.0
2024-05-14 20:36 UTC
Requires
- php: ^8.1
Requires (Dev)
- orchestra/testbench: ^9.0
- pestphp/pest: 3.x-dev
- pestphp/pest-plugin-laravel: 3.x-dev
- phpstan/phpstan: ^1.11
README
Introduction
A simple package to generate Nano IDs in Laravel.
What is a Nano ID?
A Nano ID is a URL-friendly, unique string ID.
It is similar to UUIDs, but shorter and more readable.
Nano IDs are 21 characters long by default and can be customized to be longer or shorter.
- Read more on Medium: Nano ID: A Tiny, Secure URL-friendly Unique String ID Generator
- Or on GitHub: Nano ID
Table of Contents
Features
- A facade to generate Nano IDs
- A helper function to use as alternative to the facade
- Configuration options to customize the defaults for your Nano IDs
- Prefix
- Suffix
- Alphabet
- Size
- Random string generator
- More to come
- Easily extensible with custom validation rules (like uniqueness or swear word checks)
Requirements
- PHP >= 8.0
Installation
composer require fanmade/laravel-nanoid
Usage
use Fanmade\NanoId\Facades\NanoID; echo NanoID::generate(); // Returns a Nano ID echo NanoID::generate(length: 10); // Returns a Nano ID with a length of 10 echo NanoID::generate(length: 10, prefix: 'prefix_'); // Returns a Nano ID with a length of 10 and a prefix of 'prefix_' echo NanoID::generate(suffix: '_suffix'); // Returns a Nano ID with a suffix of '_suffix' echo NanoID::generate(alphabet: '0123456789'); // Returns a Nano ID only containing numbers echo nano_id(); // The helper function accepts the same parameters as the generate method
Configuration
Publish the configuration file
php artisan vendor:publish --tag=nanoid-config
Testing
vendor/bin/pest
or
composer test
Alternatives (and inspirations)
- hidehalo/nanoid-php - The original Nano ID package for PHP. This is also used in the background of this package.
- yondifon/laravel-nanoid - More focused on adding the Nano IDs to the models.
- ttbooking/laravel-nanoid - This one has more focus on extending the string helpers.