From b9bccf8fa46f2b8df660199faf08de7a2c654e66 Mon Sep 17 00:00:00 2001 From: AfzalSabbir Date: Sat, 17 Sep 2022 14:34:24 +0600 Subject: [PATCH 1/6] Updating model - Comments added in config file --- README.md | 46 +++++++++++++++++++++++++++++++-------- config/slug-generator.php | 10 ++++----- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 99f0633..5ddb577 100644 --- a/README.md +++ b/README.md @@ -4,18 +4,49 @@ [![Travis](https://img.shields.io/travis/afzalsabbir/slug-generator.svg?style=flat-square)]() [![Total Downloads](https://img.shields.io/packagist/dt/afzalsabbir/slug-generator.svg?style=flat-square)](https://packagist.org/packages/afzalsabbir/slug-generator) - ## Install -```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. +- You can also use the `generateSlug(Model $model)` method to generate a slug. +- Publish the config file to customize the slug generation. + ```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 $slug = [ + "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 + ]; + + // ... + } + ``` + +```php +``` ## Testing @@ -25,17 +56,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/config/slug-generator.php b/config/slug-generator.php index 9eda695..6f985b1 100644 --- a/config/slug-generator.php +++ b/config/slug-generator.php @@ -1,9 +1,9 @@ "title", - "separator" => "-", - "slug-field" => "slug", - "set-on-update" => false, - "set-on-create" => true, + "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 ]; From 2fd74f5465323df22665f8574a2827fa5f0f63e9 Mon Sep 17 00:00:00 2001 From: AfzalSabbir Date: Sat, 17 Sep 2022 15:01:53 +0600 Subject: [PATCH 2/6] Updated readme.md - Renamed helper file `helpers.php` to `slug-generator-helpers.php` - Readme.md Usage updated - Changed model property from `$slug` to `$slugGenerator` --- README.md | 32 +++++++++++++++---- composer.json | 2 +- src/Traits/SlugGenerator.php | 10 +++--- ...helpers.php => slug-generator-helpers.php} | 0 4 files changed, 32 insertions(+), 12 deletions(-) rename src/{helpers.php => slug-generator-helpers.php} (100%) diff --git a/README.md b/README.md index 5ddb577..e9429a4 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,20 @@ composer require afzalsabbir/slug-generator ## Usage - Using this package is very simple. Just use the `SlugGenerator` trait in your model and it'll automatically generate a - slug for you. -- You can also use the `generateSlug(Model $model)` method to generate a slug. + 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. ```php php artisan vendor:publish --provider="AfzalSabbir\SlugGenerator\SlugGeneratorServiceProvider" --tag="config" @@ -33,7 +45,7 @@ composer require afzalsabbir/slug-generator use SlugGenerator; // ... - protected $slug = [ + 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 @@ -44,9 +56,17 @@ composer require afzalsabbir/slug-generator // ... } ``` - -```php -``` +- 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 diff --git a/composer.json b/composer.json index 607dff3..4edcf58 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "Afzalsabbir\\SlugGenerator\\": "src/" }, "files": [ - "src/helpers.php" + "src/slug-generator-helpers.php" ] }, "autoload-dev": { diff --git a/src/Traits/SlugGenerator.php b/src/Traits/SlugGenerator.php index 4d37adb..7c2b670 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 From 10669c16931f8d3586622cf09daefa0a8744ce57 Mon Sep 17 00:00:00 2001 From: AfzalSabbir Date: Sat, 17 Sep 2022 15:10:32 +0600 Subject: [PATCH 3/6] Update README.md --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e9429a4..6b77ae2 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,17 @@ -# 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) -## Install +## Installation + +You can install the package via composer: ```php composer require afzalsabbir/slug-generator From 5a6ebaf85bbe325a0b696515ba9ae16469c97ec2 Mon Sep 17 00:00:00 2001 From: AfzalSabbir Date: Sat, 17 Sep 2022 15:42:00 +0600 Subject: [PATCH 4/6] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c137ab9..0f78b29 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": [ From 296524aaf30bdfb64328d32fa0c9cd1d4bf52a48 Mon Sep 17 00:00:00 2001 From: Afzalur Rahman Sabbir <39697431+AfzalSabbir@users.noreply.github.com> Date: Sat, 17 Sep 2022 16:04:17 +0600 Subject: [PATCH 5/6] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6b77ae2..0d58b68 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ composer require afzalsabbir/slug-generator // ... } ``` -- Publish the config file to customize the slug generation. +- 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" ``` From b1d81f27693efb347c1efb26ae92aaa70ed934e5 Mon Sep 17 00:00:00 2001 From: AfzalSabbir Date: Sat, 17 Sep 2022 16:38:38 +0600 Subject: [PATCH 6/6] Cleanups --- composer.json | 8 ++--- .../{slug-generator.php => sluggenerator.php} | 0 src/SlugGeneratorFacade.php | 2 +- src/SlugGeneratorServiceProvider.php | 36 ++----------------- 4 files changed, 8 insertions(+), 38 deletions(-) rename config/{slug-generator.php => sluggenerator.php} (100%) diff --git a/composer.json b/composer.json index 0f78b29..a1a2ff4 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ }, "autoload": { "psr-4": { - "Afzalsabbir\\SlugGenerator\\": "src/" + "AfzalSabbir\\SlugGenerator\\": "src/" }, "files": [ "src/slug-generator-helpers.php" @@ -39,7 +39,7 @@ }, "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/sluggenerator.php similarity index 100% rename from config/slug-generator.php rename to config/sluggenerator.php diff --git a/src/SlugGeneratorFacade.php b/src/SlugGeneratorFacade.php index 94ac0b0..dc29f0e 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 784874e..ee7fe8d 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'); } }