laravel-slug-generator maintained by kiran99
Laravel Slug Generator
A simple Laravel package for generating URL-friendly slugs.
Installation
You can install the package via Composer:
composer require kiran99/laravel-slug-generator
The package is automatically registered through Laravel package auto-discovery.
Usage
You can generate a slug using the Slug facade:
use Kiran\SlugGenerator\Facades\Slug;
$slug = Slug::make('Hello Laravel Package');
echo $slug;
Output:
hello-laravel-package
Configuration
The package uses a configuration file:
config/slug.php
The default configuration is:
return [
'separator' => '-',
];
You can publish the configuration file to your Laravel application:
php artisan vendor:publish --tag=slug-config
After publishing, you can change the separator:
return [
'separator' => '_',
];
Then:
Slug::make('Hello Laravel Package');
will return:
hello_laravel_package
Dependency Injection
The package provides a SlugGeneratorInterface contract:
use Kiran\SlugGenerator\Contracts\SlugGeneratorInterface;
The package binds the interface to SlugService through the Laravel service container.
You can inject the interface into your own classes:
use Kiran\SlugGenerator\Contracts\SlugGeneratorInterface;
class ArticleService
{
public function __construct(
protected SlugGeneratorInterface $slugGenerator
) {
}
public function generateSlug(string $title): string
{
return $this->slugGenerator->make($title);
}
}
Laravel automatically resolves SlugService when SlugGeneratorInterface is requested.
Facade
For simple usage, you can use the facade:
use Kiran\SlugGenerator\Facades\Slug;
Slug::make('Hello World');
The facade resolves the package service through Laravel's service container.
Testing
Run the package tests with:
composer test
License
This package is open-sourced software licensed under the MIT license.