diff --git a/README.md b/README.md index 99f0633..0d58b68 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,80 @@ -# slug-generator +# SlugGenerator - Laravel Slug Generator +> A Laravel package to generate slugs for your models. [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) -[![Travis](https://img.shields.io/travis/afzalsabbir/slug-generator.svg?style=flat-square)]() +[![Build Status](https://img.shields.io/travis/afzalsabbir/slug-generator/master.svg?style=flat-square)](https://travis-ci.org/afzalsabbir/slug-generator) +[![Quality Score](https://img.shields.io/scrutinizer/g/afzalsabbir/slug-generator.svg?style=flat-square)](https://scrutinizer-ci.com/g/afzalsabbir/slug-generator) + +[![Latest Stable Version](https://img.shields.io/packagist/v/afzalsabbir/slug-generator.svg?style=flat-square)](https://packagist.org/packages/afzalsabbir/slug-generator) +[![Latest Unstable Version](https://img.shields.io/packagist/vpre/afzalsabbir/slug-generator.svg?style=flat-square)](https://packagist.org/packages/afzalsabbir/slug-generator) [![Total Downloads](https://img.shields.io/packagist/dt/afzalsabbir/slug-generator.svg?style=flat-square)](https://packagist.org/packages/afzalsabbir/slug-generator) +## Installation -## Install +You can install the package via composer: -```bash +```php composer require afzalsabbir/slug-generator ``` - ## Usage -Write a few lines about the usage of this package. - +- Using this package is very simple. Just use the `SlugGenerator` trait in your model and it'll automatically generate a + slug for you. It'll use default configuration. + ```php + // ... + use AfzalSabbir\SlugGenerator\SlugGenerator; + use Illuminate\Database\Eloquent\Model; + // ... + + class Post extends Model + { + // ... + use SlugGenerator; + // ... + } + ``` +- Publish the config file to customize the slug generation. \ + _[**Note:** if facing any issue run `php artisan vendor:publish` and find `AfzalSabbir\SlugGenerator\SlugGeneratorServiceProvider` to publish]_ + ```php + php artisan vendor:publish --provider="AfzalSabbir\SlugGenerator\SlugGeneratorServiceProvider" --tag="config" + ``` +- You can also configure the slug generation in your model. + ```php + namespace App\Models; + + use AfzalSabbir\SlugGenerator\SlugGenerator; + use Illuminate\Database\Eloquent\Model; + // ... + + class Post extends Model + { + // ... + use SlugGenerator; + + // ... + protected $slugGenerator = [ + "set-on-create" => true, // Whether to set the slug when the model is created + "set-on-update" => false, // Whether to update the slug when the target field is updated + "target-field" => "title", // The field that will be used to generate the slug + "separator" => "-", // The separator that will be used to separate the words + "slug-field" => "slug", // The field that will be used to store the slug + ]; + + // ... + } + ``` +- You can also use the `generateSlug(Model $model)` helper method to generate a slug. + ```php + use AfzalSabbir\SlugGenerator\SlugGenerator; + use App\Models\Post; + // ... + + $post = Post::find(1); + $post->title = "Hello World"; + $post->slug = generateSlug($post); + $post->save(); + ``` ## Testing @@ -25,17 +84,14 @@ Run the tests with: vendor/bin/phpunit ``` - ## Contributing Please see [CONTRIBUTING](CONTRIBUTING.md) for details. - ## Security If you discover any security-related issues, please email afzalbd1@gmail.com instead of using the issue tracker. - ## License -The MIT License (MIT). Please see [License File](/LICENSE.md) for more information. \ No newline at end of file +The MIT License (MIT). Please see [License File](/LICENSE.md) for more information. diff --git a/composer.json b/composer.json index 111903b..a1a2ff4 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "afzalsabbir/slug-generator", - "description": "Your Package Description here", + "description": "A simple package to generate slug from string for Laravel", "type": "library", "license": "MIT", "keywords": [ @@ -31,15 +31,15 @@ }, "autoload": { "psr-4": { - "Afzalsabbir\\SlugGenerator\\": "src/" + "AfzalSabbir\\SlugGenerator\\": "src/" }, "files": [ - "src/helpers.php" + "src/slug-generator-helpers.php" ] }, "autoload-dev": { "psr-4": { - "Afzalsabbir\\SlugGenerator\\Tests\\":"tests/" + "AfzalSabbir\\SlugGenerator\\Tests\\":"tests/" } }, "config": { @@ -53,10 +53,10 @@ "extra": { "laravel": { "providers": [ - "Afzalsabbir\\SlugGenerator\\SlugGeneratorServiceProvider" + "AfzalSabbir\\SlugGenerator\\SlugGeneratorServiceProvider" ], "aliases": { - "Afzalsabbir": "Afzalsabbir\\SlugGenerator\\SlugGeneratorFacade" + "SlugGenerator": "AfzalSabbir\\SlugGenerator\\SlugGeneratorFacade" } } } diff --git a/config/slug-generator.php b/config/slug-generator.php deleted file mode 100644 index 9eda695..0000000 --- a/config/slug-generator.php +++ /dev/null @@ -1,9 +0,0 @@ - "title", - "separator" => "-", - "slug-field" => "slug", - "set-on-update" => false, - "set-on-create" => true, -]; diff --git a/config/sluggenerator.php b/config/sluggenerator.php new file mode 100644 index 0000000..6f985b1 --- /dev/null +++ b/config/sluggenerator.php @@ -0,0 +1,9 @@ + true, // Whether to set the slug when the model is created + "set-on-update" => false, // Whether to update the slug when the target field is updated + "target-field" => "title", // The field that will be used to generate the slug + "separator" => "-", // The separator that will be used to separate the words + "slug-field" => "slug", // The field that will be used to store the slug +]; diff --git a/src/SlugGeneratorFacade.php b/src/SlugGeneratorFacade.php index 153b7bc..e4bb125 100644 --- a/src/SlugGeneratorFacade.php +++ b/src/SlugGeneratorFacade.php @@ -13,6 +13,6 @@ class SlugGeneratorFacade extends Facade */ protected static function getFacadeAccessor() { - return 'slug-generator'; + return 'SlugGenerator'; } } diff --git a/src/SlugGeneratorServiceProvider.php b/src/SlugGeneratorServiceProvider.php index 54161fc..b444997 100644 --- a/src/SlugGeneratorServiceProvider.php +++ b/src/SlugGeneratorServiceProvider.php @@ -14,40 +14,10 @@ class SlugGeneratorServiceProvider extends ServiceProvider */ public function boot() { - $this->mergeConfigFrom(__DIR__ . '/../config/slug-generator.php', 'sluggenerator'); + $this->mergeConfigFrom(__DIR__ . '/../config/sluggenerator.php', 'sluggenerator'); $this->publishConfig(); //$this->publishTraits(); - - // $this->loadViewsFrom(__DIR__.'/resources/views', 'slug-generator'); - // $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); - // $this->registerRoutes(); - } - - /** - * Register the package routes. - * - * @return void - */ - private function registerRoutes() - { - Route::group($this->routeConfiguration(), function () { - $this->loadRoutesFrom(__DIR__ . '/Http/routes.php'); - }); - } - - /** - * Get route group configuration array. - * - * @return array - */ - private function routeConfiguration() - { - return [ - 'namespace' => "Afzalsabbir\SlugGenerator\Http\Controllers", - 'middleware' => 'api', - 'prefix' => 'api' - ]; } /** @@ -58,7 +28,7 @@ private function routeConfiguration() public function register() { // Register facade - $this->app->singleton('slug-generator', function () { + $this->app->singleton('sluggenerator', function () { return new SlugGenerator; }); } @@ -72,7 +42,7 @@ public function publishConfig() { if ($this->app->runningInConsole()) { $this->publishes([ - __DIR__ . '/../config/slug-generator.php' => config_path('sluggenerator.php'), + __DIR__ . '/../config/sluggenerator.php' => config_path('sluggenerator.php'), ], 'config'); } } diff --git a/src/Traits/SlugGenerator.php b/src/Traits/SlugGenerator.php index f83ee8f..3f0168e 100644 --- a/src/Traits/SlugGenerator.php +++ b/src/Traits/SlugGenerator.php @@ -20,13 +20,13 @@ trait SlugGenerator public static function bootSlugGenerator(): void { static::creating(function ($model) { - if ($model->slug['set-on-create'] ?? config("sluggenerator.set-on-create")) { + if ($model->slugGenerator['set-on-create'] ?? config("sluggenerator.set-on-create")) { $model->handle($model); } }); static::updating(function ($model) { - if ($model->slug['set-on-update'] ?? config("sluggenerator.set-on-update")) { + if ($model->slugGenerator['set-on-update'] ?? config("sluggenerator.set-on-update")) { $model->handle($model); } }); @@ -41,9 +41,9 @@ public static function bootSlugGenerator(): void public function handle(Model $model): string { try { - $targetField = $model->slug['target-field'] ?? config("sluggenerator.target-field"); - $separator = $model->slug['separator'] ?? config("sluggenerator.separator"); - $slugField = $model->slug['slug-field'] ?? config("sluggenerator.slug-field"); + $targetField = $model->slugGenerator['target-field'] ?? config("sluggenerator.target-field"); + $separator = $model->slugGenerator['separator'] ?? config("sluggenerator.separator"); + $slugField = $model->slugGenerator['slug-field'] ?? config("sluggenerator.slug-field"); return $model->generateSlug(null, $slugField, $targetField, $separator); } diff --git a/src/helpers.php b/src/slug-generator-helpers.php similarity index 100% rename from src/helpers.php rename to src/slug-generator-helpers.php