afzalsabbir/slug-generator

一个用于Laravel从字符串生成slug的简单包

v0.0.1-alpha-2 2022-09-17 16:30 UTC

This package is auto-updated.

Last update: 2024-09-17 20:53:22 UTC


README

一个用于为你的模型生成slugs的Laravel包。

Software License Build Status Quality Score

Latest Stable Version Latest Unstable Version Total Downloads

安装

你可以通过composer安装此包

composer require afzalsabbir/slug-generator

使用方法

  • 使用此包非常简单。只需在你的模型中使用SlugGenerator特质,它将自动为你生成slug。它将使用默认配置。
    // ...
    use AfzalSabbir\SlugGenerator\SlugGenerator;
    use Illuminate\Database\Eloquent\Model;
    // ...
    
    class Post extends Model
    {
        // ...
        use SlugGenerator;
        // ...
    }
  • 发布配置文件以自定义slug生成。
    [注意:如果遇到任何问题,请运行php artisan vendor:publish并找到AfzalSabbir\SlugGenerator\SlugGeneratorServiceProvider以发布]
    php artisan vendor:publish --provider="AfzalSabbir\SlugGenerator\SlugGeneratorServiceProvider" --tag="config"
  • 你还可以在模型中配置slug生成。
    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
        ];
    
        // ...
    }
  • 你还可以使用generateSlug(Model $model)辅助方法来生成slug。
    use AfzalSabbir\SlugGenerator\SlugGenerator;
    use App\Models\Post;
    // ...
    
    $post = Post::find(1);
    $post->title = "Hello World";
    $post->slug = generateSlug($post);
    $post->save();

测试

使用以下命令运行测试

vendor/bin/phpunit

贡献

有关详细信息,请参阅CONTRIBUTING

安全

如果你发现任何安全相关的问题,请通过afzalbd1@gmail.com发送电子邮件,而不是使用问题跟踪器。

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件