kiran99 / laravel-slug-generator
This is my package laravel-slug-generator
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.0||^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^11.0.0||^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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 kiran99\LaravelSlugGenerator\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 kiran99\LaravelSlugGenerator\Contracts\SlugGeneratorInterface;
The package binds the interface to SlugService through the Laravel service container.
You can inject the interface into your own classes:
use kiran99\LaravelSlugGenerator\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 kiran99\LaravelSlugGenerator\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.