📜  laravel sluggable - PHP (1)

📅  最后修改于: 2023-12-03 15:17:12.988000             🧑  作者: Mango

Laravel Sluggable - PHP

Laravel Sluggable is a package for Laravel framework that allows you to easily generate slugs for your Eloquent models. Slugs are SEO-friendly URLs that help to improve the visibility and discoverability of your website.

Features
  • Automatically create slugs from any field in your model, such as title, name, or even a combination of fields
  • Customize the slug's separator, maximum length, and characters allowed
  • Provide a fallback slug in case the original one cannot be created
  • Support for UTF-8 characters and multilingual slugs
  • Generate unique slugs by appending a number to the end of duplicate slugs
  • Easily integrate with popular Laravel packages such as Eloquent and Voyager
Installation

To install Laravel Sluggable, you can use Composer:

composer require cviebrock/eloquent-sluggable

Once installed, you can publish the configuration file using the following command:

php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider" --tag="config"
Usage

To use Laravel Sluggable, simply add the Sluggable trait to your Eloquent model:

use Cviebrock\EloquentSluggable\Sluggable;

class Post extends Model
{
    use Sluggable;
    
    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

In the example above, the sluggable method returns an array with the slug key. This key defines the configuration options for the slug:

  • source: This is the field that will be used to create the slug. In this case, we are using the title field. You can also use a closure to generate the slug dynamically based on multiple fields.

Once you have added the Sluggable trait and defined the sluggable method, Laravel Sluggable will automatically generate and save the slug whenever you create or update a model instance.

Customization

There are many configuration options available to customize the behavior of Laravel Sluggable. Here are some examples:

Separator

By default, Laravel Sluggable uses a hyphen - to separate the slug words. You can customize this by adding the separator option to your sluggable method:

public function sluggable(): array
{
    return [
        'slug' => [
            'source' => 'title',
            'separator' => '_'
        ]
    ];
}

In the example above, we are using an underscore _ as the slug separator.

Maximum Length

You can limit the maximum length of the slug by adding the max_length option:

public function sluggable(): array
{
    return [
        'slug' => [
            'source' => 'title',
            'max_length' => 50
        ]
    ];
}

In the example above, we are limiting the slug to 50 characters.

Characters Allowed

You can restrict the characters allowed in the slug by adding the slugify option:

public function sluggable(): array
{
    return [
        'slug' => [
            'source' => 'title',
            'slugify' => [
                'lowercase' => true,
                'regexp' => '/[^a-zA-Z0-9]/',
                'replace' => '-'
            ]
        ]
    ];
}

In the example above, we are replacing any non-alphanumeric characters with a hyphen -.

Conclusion

Laravel Sluggable is a powerful package that simplifies the creation of SEO-friendly slugs in your Laravel applications. With its flexible configuration options and easy integration with Laravel, it's a must-have tool for any developer looking to improve the discoverability of their websites.