pragmarx/random
Cryptographically secure random generator for PHP: create random strings, integers, bytes, hex, and regex-based patterns with options for size, case, prefixes/suffixes, and raw output. Falls back to Faker (if installed) for rich random data like names and dates.
pragmarx/random package is a lightweight utility for generating random strings, numbers, and characters. It fits well in architectures requiring:
random_bytes() under the hood, though not explicitly advertised as secure for cryptographic purposes).Facade or Service Provider for consistency.composer require pragmarx/random.Str::random() or Str::uuid() in Laravel for non-cryptographic use cases.Faker alternatives) or seeders for dynamic data generation.random_bytes() internally, which is cryptographically secure, but the package itself does not explicitly guarantee suitability for cryptographic purposes (e.g., passwords, tokens for sensitive operations).composer.json (e.g., ^1.0).Str::random() with secureRandom().Facade (e.g., Random::generate()) for consistency with other helpers?Faker, Str::random())? Are there edge cases (e.g., locale-specific character sets) to address?Str::random(): More flexible (e.g., custom character sets, length constraints) but less Laravel-idiomatic.Faker: Lighter weight but lacks complex data generation (e.g., fake names, addresses).Ramsey/Uuid: Not a replacement for UUIDs, but can generate UUID-like strings.Str::random() calls with Random::generate() where flexibility is needed.RandomService facade:
// app/Providers/AppServiceProvider.php
public function boot()
{
app()->singleton('random', function () {
return new \Pragmarx\Random\Random();
});
}
Then use app('random')->generate() or bind to a facade.Str::random(10) with Random::generate(10)).composer.json and test in a staging environment.Str::random() calls in new features or non-critical paths.UNIQUE constraints) or use UUIDs for critical cases.random_bytes() is disabled or weak in the PHP config, randomness quality degrades.random_bytes is enabled and cryptographically secure in php.ini.generate() method) with clear examples in the README.Str::random() or Faker.Faker or Str::random(), migration may require justification (e.g., "This gives us more control over character sets").How can I help you explore Laravel packages today?