io238/eloquent-encoded-ids
Composer 安装命令:
composer require io238/eloquent-encoded-ids
包简介
Automatic route key encryption for Laravel Eloquent using Hashids (short, unique, non-sequential ids) with prefix support
README 文档
README
Automatic route key encryption for Laravel Eloquent using Hashids (short, unique, non-sequential IDs) with prefix support.
Background
By default Laravel uses numeric, sequential IDs for models. These numeric IDs are then also used as route keys:
route('users.show', User::find(1)) // http://app.test/user/1 route('users.show', User::find(2)) // http://app.test/user/2 route('users.show', User::find(3)) // http://app.test/user/3
This package automatically encodes the model ID, so that sequence is not externally exposed:
route('users.show', User::find(1)) // http://app.test/user/m8y78 route('users.show', User::find(2)) // http://app.test/user/p8b7v route('users.show', User::find(3)) // http://app.test/user/dvd6v
This is useful to hide sensitive app information (e.g. total number of users, invoices, etc).
Installation
You can install the package via composer:
composer require io238/eloquent-encoded-ids
Usage
In order to encode the ID of a Laravel model, simply add the HasEncodedIds trait to the model:
namespace App\Models; use Io238\EloquentEncodedIds\Traits\HasEncodedIds; class User extends Model { use HasEncodedIds; // .. }
Internally, Laravel will still use the numeric ID within controllers and also stores them as numeric IDs in the database:
class UserController extends Controller { public function show(User $user) { return $user->id; // 1 } }
Prefixes
By default this package adds a prefix to the encoded ID, which helps to identify what type of ID has been encoded.
Example: The User model has encoded IDs starting with u_, such as u_m8y78.
It uses the model name's first letter, or you can explicitly provide a prefix as a protected property of the model:
class User extends Model { use HasEncodedIds; protected $prefix = 'usr'; }
Config
This package works out-of-the-box. Nevertheless, you can publish and customize the config file with:
php artisan vendor:publish --provider="\Io238\EloquentEncodedIds\EncodedIdsProvider" --tag="config"
This is the contents of the default config file:
return [ // Minimum length of encoded IDs 'length' => 5, // Alphabet to be used to generate encoded IDs // By default this list excludes ambiguous characters 'alphabet' => '123456789abcdefghikmnpqrstuvwxyz', // Ignore uppercase/lowercase for encoded IDs 'case-insensitive' => true, // Encryption salt // Warning: changing the salt, will produce different encoded IDs 'salt' => env('APP_KEY'), // Use a prefix to the encoded ID, to be able to recognize the model that the ID belongs to 'prefix' => true, // Character used to separate the prefix from the encoded ID 'separator' => '_', ];
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review the security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
io238/eloquent-encoded-ids 适用场景与选型建议
io238/eloquent-encoded-ids 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51 次下载、GitHub Stars 达 9, 最近一次更新时间为 2020 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「encryption」 「id」 「eloquent」 「hashids」 「io238」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 io238/eloquent-encoded-ids 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 io238/eloquent-encoded-ids 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 io238/eloquent-encoded-ids 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A simple PHP class to encrypt a string and decrypt an encrypted string
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Encryption with AES-256 and HMAC-SHA256
统计信息
- 总下载量: 51
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-30