承接 rossbearman/eloquent-calamari 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rossbearman/eloquent-calamari

Composer 安装命令:

composer require rossbearman/eloquent-calamari

包简介

Obfuscate incrementing IDs with Sqids for Laravel and Eloquent.

README 文档

README

Latest Stable Version Test Status Analysis Status MIT Licensed

Eloquent Calamari integrates the Sqids1 algorithm into Laravel and Eloquent, enabling you to seamlessly use obfuscated, unique IDs in place of your internal auto-incrementing IDs.

  • Obfuscate auto-incrementing IDs with short, unique identifiers
  • Unique Sqids across models, ID 1 will be represented differently on every model
  • Optional route model binding with SqidBasedRouting
  • Transparently handle non-canonical IDs

example.com/link/2fC37YMkO === Link::find(1)

example.com/video/TaRfL1RAK === Video::find(1)

Getting Started

Require this package with Composer.

composer require rossbearman/eloquent-calamari

Add the HasSqid and SqidBasedRouting traits to your models.

use RossBearman\Sqids\HasSqid;
use RossBearman\Sqids\SqidBasedRouting;

class Customer extends Model
{
    use HasSqid, SqidBasedRouting;
}

Create a route for your model.

Route::get('/customer/{customer}', fn (Customer $customer) => $customer);
$customer = Customer::create(['name' => 'Squidward']);

$customer->id; // 1
$customer->sqid; // 3irWXI2rFV

example.com/customer/3irWXI2rFV now returns the Customer details.

Querying

Common query methods are also available.

Customer::findBySqid($sqid);

Customer::findBySqidOrFail($sqid);

Customer::whereSqid($sqid)->get();

Customer::whereSqidIn($sqids)->get();

Customer::whereSqidNotIn($sqids)->get();

Representation

By default the Sqid is not included in the model's toArray() or toJson() output and the ID is not hidden from these. You can use Eloquent's appends and hidden properties to achieve this.

class Customer extends Model
{
    use HasSqid, SqidBasedRouting;
    
    protected $appends = ['sqid'];
    protected $hidden = ['id'];
}

Custom Routing

You can take advantage of SqidBasedRouting while still having a different default binding by overriding the model's getRouteKeyName() method.

class Customer extends Model
{
    use HasSqid, SqidBasedRouting;
    
    public function getRouteKeyName(): string
    {
        return 'id';
    }
}
// Routes by ID by default
Route::get('/admin/customer/{customer}', fn (Customer $customer) => $customer);

// Routes by Sqid when specified
Route::get('/customer/{customer:sqid}', fn (Customer $customer) => $customer);

Configuration

By default, Eloquent Calamari generates a random alphabet for each model, by shuffling the default alphabet with the Xoshiro256StarStar algorithm, seeded with a combination of the name of the model and the key set in the config.

This ensures that entities of different models with the same ID will have a unique Sqid, however it is fragile to the model name or app key being changed. If either of these are changed, Sqids will no longer resolve back to the same ID.

Important

It is highly recommended that you explicitly set a pre-shuffled alphabet for each model using the sqids.alphabets config key, which will disable the shuffling behaviour for that model.

Setting alphabets

Start by publishing the sqids.php config file to your config directory.

php artisan vendor:publish --provider="RossBearman\Sqids\SqidsServiceProvider"

Then generate a new alphabet for your model.

php artisan sqids:alphabet "App\Models\Customer"

You can also generate alphabets for multiple models at once.

php artisan sqids:alphabet "App\Models\Customer" "App\Models\Order" "App\Models\Invoice"

Follow the instructions provided by the command to add the new keys to your config/sqids.php and .env files.

Confirm alphabet is being used

To ensure that Eloquent Calamari is using the expected alphabet for a specific model, use the following command.

php artisan sqids:check

This will list all the models that have successfully been registered in the config and whether the class string can be resolved to a class in your application.

Setting minimum Sqid lengths

By default, all Sqids will be a minimum of 10 characters. You can adjust this for each model by assigning different values (to a minimum of 3) to the sqids.min_lengths config array.

    'min_lengths' => [
        App\Model\Customer::class => 20,
        App\Model\Order::class => 8,
        App\Model\Invoice::class => 30,
    ],

The maximum length of a Sqid is dependent on the input ID and the alphabet used. A more varied alphabet (upper and lower case letters, numbers and symbols) will result in shorter Sqids.

Canonical Sqids

By design, multiple Sqids can resolve to the same number, however Eloquent Calamari will always return the same Sqid for a given number. Furthermore, this is the only Sqid that can be used to access an entity, and any other Sqid that would normally resolve to the same number will be rejected.

This check can be disabled on a per-model basis by adding an entry to the sqids.canonical_checks config array.

    'canonical_checks' => [
        App\Model\Customer::class => false,
    ],

Development

PHPUnit tests:

composer test

PHPStan analysis:

composer analyse

Pint linting:

composer lint

Security

Please email Ross Bearman ross@rossbearman.co.uk if you have discovered a vulnerability in this package.

License

MIT License (MIT). Please see LICENSE.md for more information.

Footnotes

  1. Sqids is the latest version of the Hashids algorithm, redesigned to accomodate custom blocklists and a better encoding scheme.

rossbearman/eloquent-calamari 适用场景与选型建议

rossbearman/eloquent-calamari 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.33k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2024 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「eloquent」 「hashids」 「sqids」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rossbearman/eloquent-calamari 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rossbearman/eloquent-calamari 我们能提供哪些服务?
定制开发 / 二次开发

基于 rossbearman/eloquent-calamari 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7.33k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-15