laravel-pulse/sluggish

为任何Laravel项目生成slug、uuid和唯一数字的完整解决方案。

v2.1 2024-03-01 17:26 UTC

This package is not auto-updated.

Last update: 2024-09-23 18:42:04 UTC


README

Sluggish是一个用于生成唯一slug、自定义slug、唯一ID、UUID、带前缀的顺序号的Laravel库包。

要求

PHP版本:8.0或更高 Laravel版本:8,9,10,11(与这些版本兼容)

安装

使用此包,运行以下命令。

composer require laravel-pulse/sluggish

配置

发布sluggish配置文件

php artisan vendor:publish --provider="LaravelPulse\Sluggish\SluggishServiceProvider"

此命令将在config/sluggish.php中发布默认配置

<?php

return [
    // The separator used to generate slugs. For example, if the separator is '-', a title like "Hello World" will be transformed to "hello-world".
    'separator' =>  '-',

    // If a slug cannot be generated using the configured separator, this fallback separator will be used. For example, if the fallback separator is '_', "Hello World" will be transformed to "hello_world" if the primary separator fails.
    'fallback-separator' =>  '_',

    // The maximum number of attempts to generate a unique slug. If a generated slug already exists, a new attempt will be made up to the specified limit.
    'max-attempts' => 100,

    // The default value to be used if no value is provided.
    'default-value' =>  '4',
];

用法

最好的事情是您可以按照需求在Sluggish的任何地方使用它。

例如,控制器、模型、特质、命令、工作、等。

Sluggish需要哪些属性

Sluggish需要4个强制参数,以便生成唯一的slug或UUID或序列号

1. string $type - 此参数应包含您想从Sluggish生成的内容。它可以是字符串。Sluggish只接受三种类型,一种是"slug","uuid"和"sequence"。

2. string $parameter - 此参数应包含什么或哪个字符串或输入应转换为slug / uuid /序列号。例如"Hello world"或$request->title或您的自定义字段。

3. object $model - 此参数应包含模型。Sluggish将执行查询并返回相应的唯一slug / uuid /序列号。您可以使用任何模型,例如User::class,Post::class等。

4. string $modelField - 此参数应包含您想要保持唯一的对应模型字段。它可以是你自定义的任何字段。您可以使用任何自定义字段,例如"slug"、"username"、"unique_id"或"unique_key"。

如果您想更新任何自定义字段的记录,只需传递ID。Sluggish也将接受ID字段。

您需要按此顺序传递,否则Sluggish可能会抛出错误

//  First Parameter : Model Class Name (String)
//  Second Parameter: Field name from which the slug should be generated (String).
//  Third Parameter : Model::class
//  Fourth Parameter: ModelField name that contains the data for generated the slug.
// Fifth Parameter: ID of any record. it is used for updating any existing record


Sluggish::generate($type, $parameter, $model, $modelField, $id = optional);

生成Slug

use LaravelPulse\Sluggish\Sluggish;

// create slug
public function store(Request $request)
{
    $post = Post::create([
        'title' => $request->title,
        'slug' => Sluggish::generate('slug', $request->title, Post::class, 'slug') // hello-world , hello-world-1, hello-world-2 .....
    ]);

    return redirect()->route('posts.index');
}

// update slug
public function update(Request $request,Post $post)
{
    $post->update([
        'title' => $request->title,
        'slug' => Sluggish::generate("slug", $request->title, Post::class, 'slug', $post)
        // if it get same id and same title  return existing slug
        // otherwise
        // hello-world , hello-world-1, hello-world-2 .....
    ]);

    return redirect()->route('posts.index');
}

生成UUID

use LaravelPulse\Sluggish\Sluggish;

// create uuid
public function store(Request $request)
{
    return Sluggish::generate('uuid', 2, Post::class, 'unique_id');

    $post = Post::create([
        'title' => $request->title,
        'unique_id' => Sluggish::generate('uuid', "order", Post::class, 'unique_id'), // 9b75b69a-a146-40c3-a4dc-4701204fbfd9
    ]);

    return redirect()->route('posts.index');
}

Example 1)  9b75b69a-a146-40c3-a4dc-4701204fbfd9
return Sluggish::generate('uuid', "order", Post::class, 'uuid');

Example 2)  65e1d5936027d
return Sluggish::generate('uuid', "time", Post::class, 'uuid');

Example 3)  dc542c3a-992b-4338-8a73-38f027e2a923
return Sluggish::generate('uuid', "hexad", Post::class, 'uuid');

Example 4)  prefix-65e1d5ff5201a7.85005484
return Sluggish::generate('uuid', "prefix-", Post::class, 'uuid');

Example 5)  202403011709299240
return Sluggish::generate('uuid', "datetime", Post::class, 'uuid');

Example 6)  9501709299298
return Sluggish::generate('uuid', "random", Post::class, 'uuid');

Example 7)  8505 (4 digits OTP format)
return Sluggish::generate('uuid', "random-4", Post::class, 'uuid');


### you can also define your own length
return Sluggish::generate('uuid', "random-5", Post::class, 'uuid'); // 84047 (5 digits OTP format)

return Sluggish::generate('uuid', "random-6", Post::class, 'uuid'); // 640470 (6 digits OTP format)

return Sluggish::generate('uuid', "random-7", Post::class, 'uuid'); // 2404706

return Sluggish::generate('uuid', "random-8", Post::class, 'uuid'); // 77346768

return Sluggish::generate('uuid', "random-9", Post::class, 'uuid'); // 362184146

return Sluggish::generate('uuid', "random-10", Post::class, 'uuid'); // 2363990894

return Sluggish::generate('uuid', "random-11", Post::class, 'uuid'); // 16497741413

return Sluggish::generate('uuid', "random-12", Post::class, 'uuid'); // 546406371883

Example 8)  $2y$12$XZojOl5wvjUAG.YVDCwyVu/sELFbhpQsWprZblt/HaofTzfnQPiam
return Sluggish::generate('uuid', "hash", Post::class, 'uuid');

### you can also pass an integer like this (1 to 255)
Example 9) 5416101709302967
return Sluggish::generate('uuid', 2, Post::class, 'uuid');

生成带前缀的数字序列

use LaravelPulse\Sluggish\Sluggish;

public function store(Request $request)
    {
        $post = Post::create([
            'title' => $request->title,

            // you can change the prefix as per your need like "ORD"
            // if no post record found inside database -> SH-0000
            // if post record found inside database -> SH-1000
            // if you define 1 to n value for last key -> [for 1 - SH-0 , for 2 - SH-00, for 3 - SH-000 , for 4 - SH-0000,  by default the value is 4 for this reason you get SH-0000]

            'serial_key' => Sluggish::generate('sequence', "SH-", Post::class, 'serial_key', 4),

            // you can also try like this
            'serial_key' => Sluggish::generate('sequence', "SH-", Post::class, 'order_id'); // SH-0000

            // remember if you insert any data using one of those methods you should migrate::fresh to try another otherwise both of those not working properly.
        ]);

        return redirect()->route('posts.index');
    }

错误

如果没有任何参数,您可能会得到一个异常

// if any parameter is not specified
// if given parameter is unordered

贡献

欢迎提交拉取请求。对于重大更改,请首先提交一个问题以讨论您想进行哪些更改。

请确保根据需要更新测试。

许可证

MIT