marvin255/random-string-generator
A lightweight PHP package for generating random strings with configurable length and character sets. Ideal for access tokens, passwords, and unique identifiers, with optional Laravel integration and a simple, framework-agnostic API.
This package is Symfony-oriented, so treat it as a plain PHP library in Laravel rather than an integrated framework component. There is no Laravel service provider or facade, and nothing will be auto-discovered.
composer require marvin255/random-string-generator
Then verify what actually landed:
composer show marvin255/random-string-generator
composer why marvin255/random-string-generator
Read the package README first — it is the only reliable source for the exact namespace, constructor signature, and whether it ships a Symfony Bundle class (you can ignore any *Bundle.php and DependencyInjection/ directory in a Laravel app). Look for the primary generator class and any character-set/alphabet constants; that is the entire surface area you will use day-to-day.
Your first use case is almost certainly short, human-transmittable codes: invite codes, coupon codes, order reference numbers, or test fixtures. Start with a single call in a tinker session before wiring anything up:
php artisan tinker
>>> $g = new \Marvin255\RandomStringGenerator\Generator(/* args from README */);
>>> $g->generate();
Wrap it immediately (see below) so the rest of your application never names the package directly — with 1 star and 0 dependents, the package is a dependency you want to be able to drop in an afternoon.
Bind behind a contract. This is the single most important pattern for a low-adoption dependency. Define your own interface, implement it with the package, and bind it in a service provider. Swapping to Str::random() or Random\Randomizer later becomes a one-line change.
// app/Support/Random/CodeGenerator.php
interface CodeGenerator
{
public function generate(int $length = 16): string;
}
// app/Support/Random/PackageCodeGenerator.php
final class PackageCodeGenerator implements CodeGenerator
{
public function __construct(private readonly \Marvin255\RandomStringGenerator\Generator $inner) {}
public function generate(int $length = 16): string
{
// map $length onto whatever the package calls it
return $this->inner->generate();
}
}
// app/Providers/AppServiceProvider.php
$this->app->singleton(\Marvin255\RandomStringGenerator\Generator::class, fn () => new \Marvin255\RandomStringGenerator\Generator(/* config */));
$this->app->bind(CodeGenerator::class, PackageCodeGenerator::class);
Drive alphabets from config. Do not scatter character sets through the codebase. Put them in config/random.php and resolve them via config('random.alphabets.coupon'). A code you show to a user (avoid 0/O, 1/l/I) and a code you put in a URL (URL-safe base62/base64url) are different requirements, and the package's default alphabet is unlikely to satisfy both.
Uniqueness loop, not random guessing. Randomness does not guarantee uniqueness. Add a unique index and retry on collision, with a bounded attempt count so a misconfigured alphabet fails loudly instead of hanging a request:
public function uniqueCode(int $maxAttempts = 5): string
{
for ($i = 0; $i < $maxAttempts; $i++) {
$code = $this->generator->generate(10);
if (! Coupon::where('code', $code)->exists()) {
return $code;
}
}
throw new \RuntimeException('Code space exhausted — widen the alphabet or length.');
}
Use it in factories and seeders for realistic fixture data (Order::factory() reference fields), and in tests where you need varied strings but not cryptographic quality. If the package accepts an injectable random engine or seed, use that to make tests deterministic; otherwise assert on shape (assertMatchesRegularExpression('/^[A-Z0-9]{10}$/', $code)) rather than exact values.
Batch generation: if you need hundreds of codes (backfill, import), generate in PHP and insert in chunks; do not instantiate the generator per row inside a loop that also hits the database.
Keep it out of the auth path. Password reset tokens, API keys, and remember-me tokens should come from random_bytes()-backed sources — Laravel's Str::random() is already random_bytes-based, and Str::password() exists for credentials. Only route these through the package if its README explicitly documents a CSPRNG backend.
length * log2(alphabetSize) before choosing parameters.utf8mb4_unicode_ci collation treats aB and Ab as equal, so your unique index collides far earlier than your entropy estimate suggests. Either use a binary/case-sensitive column or restrict the alphabet to one case.config/random.php and pass them to the constructor manually. Laravel will never read config/packages/*.yaml.composer why output — a Symfony-flavored package may pull symfony/* components you otherwise would not load. For a single string generator that is often a poor trade against Str::random().composer.json comment or ADR noting why.random_int()/random_bytes() (or PHP 8.2's Random\Randomizer) rather than mt_rand(). Grep the vendor source: grep -rn "mt_rand\|rand(" vendor/marvin255/random-string-generator/src.CodeGenerator in TestCase::setUp() that returns a predictable sequence — this avoids flaky assertions on unique-constraint columns and makes feature tests readable.PrefixedCodeGenerator that adds ORD- in front of whatever the inner generator returns costs three lines and requires no knowledge of the package internals.How can I help you explore Laravel packages today?