aferrandini/urlizer
Laravel-friendly URL slug generator that “urlizes” strings into clean, readable slugs with sensible transliteration and customization options. Ideal for turning titles or names into SEO-ready URLs consistently across your app.
Installation:
composer require aferrandini/urlizer
Add to config/app.php under providers (if not auto-discovered):
Aferrandini\Urlizer\UrlizerServiceProvider::class,
Publish Config (optional):
php artisan vendor:publish --provider="Aferrandini\Urlizer\UrlizerServiceProvider"
Config file: config/urlizer.php (default: lowercase, hyphen separator, no accents).
First Use Case: Generate a slug from a title in a controller or blade:
use Aferrandini\Urlizer\Facades\Urlizer;
$slug = Urlizer::make('Hello World!'); // Output: "hello-world"
Urlizer::make($string) for quick usage.config/urlizer.php for custom rules (e.g., separator, case).Slug Generation:
$slug = Urlizer::make('Café au Lait'); // "cafe-au-lait" (default)
Urlizer::setOptions(['separator' => '_', 'lowercase' => false]);
$slug = Urlizer::make('Test String'); // "Test_String"
Laravel Integration:
class Post extends Model {
public function getSlugAttribute() {
return Urlizer::make($this->title);
}
}
Route::get('/posts/{slug}', [PostController::class, 'show']);
// Generate slugs dynamically in controllers:
$slug = Urlizer::make(request('title'));
Batch Processing:
$titles = ['Post 1', 'Post 2!'];
$slugs = collect($titles)->map(fn($title) => Urlizer::make($title));
Transliteration:
Enable in config (transliterate: true) to convert special chars (e.g., ñ → n).
Urlizer::setOptions(['transliterate' => true]);
$slug = Urlizer::make('Resumé'); // "resume"
Custom Rules: Extend the service provider to add pre-processing:
// app/Providers/UrlizerServiceProvider.php
public function boot() {
Urlizer::extend(function($string) {
return str_replace(['&', ''], '', $string); // Remove ampersands
});
}
Validation: Combine with Laravel validation for slug uniqueness:
$validator = Validator::make($request->all(), [
'slug' => 'required|unique:posts|string',
]);
$slug = Urlizer::make($request->title);
Outdated Package:
Config Overrides:
config/urlizer.php) applies to all calls. Reset with:
Urlizer::setOptions([]); // Revert to defaults
Transliteration Quirks:
symfony/string or ocramius/package-versions:
use Symfony\Component\String\UnicodeString;
$normalized = UnicodeString::from($string)->lower()->toString();
Separator Collisions:
-) in input strings may cause double separators:
Urlizer::make('Hello-world'); // "hello-world" (default)
// Fix: Pre-process or adjust config.
Inspect Output: Log intermediate steps to debug:
$steps = [
'original' => $string,
'lowercase' => mb_strtolower($string),
'transliterated' => Urlizer::transliterate($string),
'final' => Urlizer::make($string),
];
Edge Cases:
preg_replace('/[^\P{C}\p{L}]/u', '', $string)).substr(Urlizer::make($string), 0, 50)).Custom Transliteration:
Override the transliterate() method in a subclass:
class CustomUrlizer extends \Aferrandini\Urlizer\Urlizer {
protected function transliterate($string) {
// Custom logic here
return parent::transliterate($string);
}
}
Event Hooks: Listen for slug generation (if the package supports events; otherwise, wrap calls):
event(new SlugGenerated($original, $slug));
Database Indexes: Ensure slug fields are indexed for performance:
Schema::table('posts', function (Blueprint $table) {
$table->string('slug')->unique();
});
How can I help you explore Laravel packages today?