Git Product home page Git Product logo

laravel-slug-generator's Introduction

Laravel-slug-generator

Laravel Slug Generator with Multi Language Support.


Installation

composer require sujan97825/laravel-slug-generator

Configuration

Service Provider Registration In config/app.php, add in providers array -

'providers' => [
    // ...
    Sujan\\LaravelSlugGenerator\\SlugGeneratorServiceProvider::class,
    // ...
],

Facade Class Alias Add in aliases array -

'aliases' => Facade::defaultAliases()->merge([
    // ...
    'SlugGenerator' => Sujan\LaravelSlugGenerator\Facades\SlugGenerator::class,
    // ...
])->toArray(),

Use from Controller

Import first the SlugGenerator facade

use Sujan\LaravelSlugGenerator\Facades\SlugGenerator;

Example #01- Post unique slug from title

Let's assume, we have in Post class, we've added slug column which is unique. Now, if we passed title and generate slug from that, then -

use App\Models\Post;

// First time create post with title
SlugGenerator::uniqueSlug(Post::class, 'সম্পন্ন !#$%$%^ হল বঙ্গবন্ধু&**( টানেলের //? প্রথম টিউবের )()(**@%$^&*( কাজ  ', 'slug');
// Output: সম্পন্ন-হল-বঙ্গবন্ধু-টানেলের-প্রথম-টিউবের-কাজ

// Second time create post with title
SlugGenerator::uniqueSlug(Post::class, 'সম্পন্ন !#$%$%^ হল বঙ্গবন্ধু&**( টানেলের //? প্রথম টিউবের )()(**@%$^&*( কাজ  ', 'slug');
// Output: সম্পন্ন-হল-বঙ্গবন্ধু-টানেলের-প্রথম-টিউবের-কাজ-1

// Third time create post with title
SlugGenerator::uniqueSlug(Post::class, 'সম্পন্ন !#$%$%^ হল বঙ্গবন্ধু&**( টানেলের //? প্রথম টিউবের )()(**@%$^&*( কাজ  ', 'slug');
// Output: সম্পন্ন-হল-বঙ্গবন্ধু-টানেলের-প্রথম-টিউবের-কাজ-2

Example #02 - Post general slug from title / Not Unique

// First time create post with title
SlugGenerator::generalSlug('সম্পন্ন !#$%$%^ হল বঙ্গবন্ধু&**( টানেলের //? প্রথম টিউবের )()(**@%$^&*( কাজ  ');
// Output: সম্পন্ন-হল-বঙ্গবন্ধু-টানেলের-প্রথম-টিউবের-কাজ

// Second time create post with title
SlugGenerator::generalSlug('সম্পন্ন !#$%$%^ হল বঙ্গবন্ধু&**( টানেলের //? প্রথম টিউবের )()(**@%$^&*( কাজ  ');
// Output: সম্পন্ন-হল-বঙ্গবন্ধু-টানেলের-প্রথম-টিউবের-কাজ

API Docs

Generate method -

SlugGenerator::uniqueSlug($model, $value, $field, $separator);
SlugGenerator::generalSlug($value,$separator);
/**
 * Generate a Unique Slug.
 *
 * @param object $model
 * @param string $value
 * @param string $field
 * @param string $separator
 *
 * @return string
 * @throws \Exception
 */
public function uniqueSlug(
    $model,
    $value,
    $field,
    $separator = null
): string
/**
 * Generate a general Slug.
 *
 * @param string $value
 * @param string $separator
 *
 * @return string
 */
public function generalSlug(
    $value,
    $separator = null
): string

Publish configuration

php artisan vendor:publish --provider="Sujan\LaravelSlugGenerator\SlugGeneratorServiceProvider"

Configurations

return [
    /*
    |--------------------------------------------------------------------------
    | Slug default separator.
    |--------------------------------------------------------------------------
    |
    | If no separator is passed, then this default separator will be used as slug.
    |
    */
    'separator' => '-',

    /*
    |--------------------------------------------------------------------------
    | Slug max count limit
    |--------------------------------------------------------------------------
    |
    | Default 100, slug will generated like
    | test-1, test-2, test-3 .... test-100
    |
    */
    'max_count' => 100,
];

Similar Package & Inspired from

https://github.com/ManiruzzamanAkash/laravel-unique-slug-generator

Contribution

You're open to create any Pull request.

laravel-slug-generator's People

Contributors

shekhsujan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.