PHPackages                             dllobell/nanoid - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. dllobell/nanoid

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

dllobell/nanoid
===============

Lightweight, framework-agnostic nanoid generation for PHP

111PHPCI passing

Since Jan 3Pushed 4mo agoCompare

[ Source](https://github.com/dllobell/php-nanoid)[ Packagist](https://packagist.org/packages/dllobell/nanoid)[ RSS](/packages/dllobell-nanoid/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Nano ID
=======

[](#nano-id)

[![Total Downloads](https://camo.githubusercontent.com/356f90f9864287b85e5c2b1f875e56112a6d281355ec2001f4156e869b96bef6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646c6c6f62656c6c2f6e616e6f6964)](https://packagist.org/packages/dllobell/nanoid)[![Latest Stable Version](https://camo.githubusercontent.com/8162e905d886d9a51109d3e8c8b65e7d21dffd85e59c9112f049df59d8272da9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646c6c6f62656c6c2f6e616e6f6964)](https://packagist.org/packages/dllobell/nanoid)[![License](https://camo.githubusercontent.com/e0d8629568d95fbf558ed2aa1d5f035d63bcb2ce6dae0e0d25a6e7c13190ec48/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f646c6c6f62656c6c2f6e616e6f6964)](https://packagist.org/packages/dllobell/nanoid)[![PHP Minimum Version](https://camo.githubusercontent.com/9aa64716fde031dce14de7e41b3de4ede7cdefaadde38d45264e11b103abc184/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e342b2d3737374242343f6c6f676f3d706870)](https://php.net)

Lightweight, framework-agnostic Nano ID generation for PHP.

Nano ID is a tiny, secure, URL-friendly, unique string ID generator, ported from the original Javascript project [ai/nanoid](https://github.com/ai/nanoid).

Requirements
------------

[](#requirements)

- PHP 8.4 or higher

Installation
------------

[](#installation)

Install via [Composer](https://getcomposer.org):

```
composer require dllobell/nanoid
```

Usage
-----

[](#usage)

### Quick start

[](#quick-start)

```
use Dllobell\NanoId\NanoIdGenerator;

$generator = NanoIdGenerator::create();

$id = $generator->generate(); // "Xy7z_9A-mK2jLp4qR5sTv"
```

### Custom size

[](#custom-size)

You can specify the size of the ID when generating it:

```
$id = $generator->generate(10); // "aB3dE5fG7h"
```

Or set a default size when creating the generator:

```
$generator = NanoIdGenerator::create(defaultSize: 10);

$id = $generator->generate(); // "kL9mN8oP7q"
```

### Custom alphabet

[](#custom-alphabet)

You can specify a custom alphabet when creating the generator:

```
$generator = NanoIdGenerator::create(alphabet: '0123456789');

$id = $generator->generate(); // "857201493620581749302"
```

> **Note:** Alphabets must contain between 2 and 256 unique characters. Duplicate characters are not allowed.

#### Using the `AlphabetValue` interface

[](#using-the-alphabetvalue-interface)

You can pass an object implementing the `AlphabetValue` interface. This is particularly useful when using enums to create custom alphabet sets:

```
use Dllobell\NanoId\AlphabetValue;
use Dllobell\NanoId\NanoIdGenerator;

enum MyAlphabets: string implements AlphabetValue
{
    case Uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    case Lowercase = 'abcdefghijklmnopqrstuvwxyz';
    case Numeric = '0123456789';

    public function value(): string
    {
        return $this->value;
    }
}

$generator = NanoIdGenerator::create(alphabet: MyAlphabets::Numeric);
```

#### Automatic generation

[](#automatic-generation)

If you want to use a set of predefined alphabets or manage your own via `composer.json`, you can use the [Nano ID Composer Plugin](https://github.com/dllobell/php-nanoid-plugin):

```
composer require dllobell/nanoid-plugin
```

This plugin automatically generates an `Alphabets` enum that implements the `AlphabetValue` interface whenever you run `composer install`, `update`, or `dump-autoload`.

You can also trigger the generation manually using the following command:

```
composer nanoid:generate-alphabets
```

Then use it in your code:

```
use Dllobell\NanoId\Alphabets;
use Dllobell\NanoId\NanoIdGenerator;

$generator = NanoIdGenerator::create(alphabet: Alphabets::Uppercase);
```

By default, it includes the following set of common alphabets:

NameValue`Default``0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-``Uppercase``ABCDEFGHIJKLMNOPQRSTUVWXYZ``Lowercase``abcdefghijklmnopqrstuvwxyz``Numeric``0123456789``Alphanumeric``0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz``HexadecimalUppercase``0123456789ABCDEF``HexadecimalLowercase``0123456789abcdef``NoLookalikes``346789ABCDEFGHJKLMNPQRTUVWXYabcdefghijkmnpqrtwxyz`You can also define your own custom alphabets in your `composer.json` file:

```
{
    "extra": {
        "nanoid": {
            "alphabets": {
                "Vowels": "aeiou"
            }
        }
    }
}
```

You can then use it in your code using the same `Alphabets` enum:

```
use Dllobell\NanoId\Alphabets;
use Dllobell\NanoId\NanoIdGenerator;

$generator = NanoIdGenerator::create(alphabet: Alphabets::Vowels);
```

### Custom Random Bytes Generator

[](#custom-random-bytes-generator)

By default, the `NativeRandomBytesGenerator` is used for generating random bytes, you can inject a custom random bytes generator by implementing the `RandomBytesGenerator` interface:

```
use Dllobell\NanoId\RandomBytesGenerator;

final readonly class MyRandomBytesGenerator implements RandomBytesGenerator
{
    public function generate(int $size): array
    {
        // Return an array of random integers
        return [/* ... */];
    }
}

$generator = NanoIdGenerator::create(randomBytesGenerator: new MyRandomBytesGenerator());
```

### Deterministic generation

[](#deterministic-generation)

The built-in `NativeRandomBytesGenerator` accepts a custom `Engine` for deterministic/seeded generation, useful for testing:

```
use Dllobell\NanoId\NanoIdGenerator;
use Dllobell\NanoId\RandomBytesGenerator\NativeRandomBytesGenerator;
use Random\Engine\Mt19937;

$generator = NanoIdGenerator::create(
    randomBytesGenerator: new NativeRandomBytesGenerator(
        engine: new Mt19937(seed: 42),
    ),
);

$id = $generator->generate(); // Always produces the same ID for the same seed
```

Credits
-------

[](#credits)

The Nano ID generation algorithm is ported from the [ai/nanoid](https://github.com/ai/nanoid) project.

License
-------

[](#license)

The MIT License (MIT). See the [license file](LICENSE) for more information.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance52

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/1ce7b6c97be6045063ccacafcc49a200b1066d5dfd5a125aec669687c1b8effb?d=identicon)[dllobell](/maintainers/dllobell)

---

Top Contributors

[![dllobell](https://avatars.githubusercontent.com/u/49846756?v=4)](https://github.com/dllobell "dllobell (1 commits)")

### Embed Badge

![Health badge](/badges/dllobell-nanoid/health.svg)

```
[![Health](https://phpackages.com/badges/dllobell-nanoid/health.svg)](https://phpackages.com/packages/dllobell-nanoid)
```

###  Alternatives

[pwm/datetime-period

An implementation of the datetime period type for working with temporal intervals

679.0k](/packages/pwm-datetime-period)[abhi1693/yii2-sms

Send Free Sms for Yii2

171.9k](/packages/abhi1693-yii2-sms)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
