承接 davide7h/encoded-ids 相关项目开发

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

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

davide7h/encoded-ids

Composer 安装命令:

composer require davide7h/encoded-ids

包简介

An easy way to mask your IDs when exposing them in routes, APIs or other scenarios when you don't want the user to see the actual ID of your database records

README 文档

README

Logo

ID Masking for your Eloquent Models

MIT License Stability Latest Version Stability

Documentation, Installation, and Usage Instructions

What It Does

This package allows you to use a convenient Trait to instantly enable all your Eloquent Models with encoded ids for you to use in routes and api endpoints without exposing to your users the real IDs for your database records.

Requirements

This package is under active development, therefore its requirements may be subject to change. At the moment it's been successfully tested on:

  • Laravel ^8.x | ^9.x | ^10.x
  • PHP >= 8.1

Important Security Notes

This package requires Sqids package for hashing the numeric IDs. It will be automatically installed.

It's important to reiterate what the authors of the Sqids package clearly state on their website: this is not an encryption library, and the generated IDs can still be deciphered if an attacker were to find out what alphabet was used to generate it. It only serves a cosmetic purpose, rendering IDs less obvious and more hard to guess, but not cryptographically encrypted.

Do not use it to hide sensitive information!

Installation

Just run composer require davide7h/encoded-ids

This will download and extract the package and its dependencies (Sqids). The auto-discovery feature will automatically register the package ServiceProvider.

Generating Alphabet and publishing configurations

php artisan encoded-ids:install.

This command will generate a randomly shuffled alphabet that will be used to encode and decode your IDs.

It will also publish the config file config/encoded-ids.php. Here you will find:

  • the newly generated alphabet (in case you need to alter it or replace it with an entirely new one)
  • the padding key, used to specify a minimum length for the encoded id (default: 8char)

That's it, you're good to go!

Usage

Encoding Model IDs

Providing your Models with the ability to display a masked version of their actual IDs is as simple as using the HasEncodedId Trait:

<?php

namespace App\Models;

...
use Davide7h\EncodedIds\Traits\HasEncodedId;
...

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, HasEncodedId;
    
    ...
}

Now your model will feature the encoded_id attribute, which you can use to access a masked version of your model's ID:

$user = User::first();
echo $user->id;         //example output: 1
echo $user->encoded_id; //example output: "ULwu08NB"

Retrieving Models

The package provides a convenient way to query the Database for records using the encoded_id attribute, without any need for decoding it. Just call the find or findOrFail methods on your model's class, like you would normally do using the acutal (numeric) IDs for your records as parameters, but with the option to also use their masked version (as a string):

User::find(1)          //returns the User model with ID = 1;
User::find("ULwu08NB") //also returns the User with ID = 1;

Decoding IDs

As discussed before, there is no need for explicitly decoding your IDs before queueing the Database. Should the need for manual decoding ever arise, though, the package provides an EncodingService class that can be used anywhere in your code to encode or decode integer numbers for any reason you might think of.

...
use Davide7h\EncodedIds\Services\EncodingService;
...

EncodingService::encode(1);          //example output: "ULwu08NB"
EncodingService::decode("ULwu08NB"); //example output: 1;

Please note that the encode method of the EncodingService class expects an Int as a parameter and returns a String, while the decode method expects a String and returns the decoded Int if the given string is a valid encoding, null otherwise.

Parametric routes

Using Dependency Injection to fetch the actual model from a parametric route is pretty handy, but unfortunately it only works when the parameter used to populate the route exists as a column in your model's database table. When using computed attributes (like encoded_id )the workaround used to be giving up on the dependency injection and just query the database for the desired model after decoding it inside the controller:

//Route file:
Route::get('/users/{encoded_id}', [UserController::class, 'getUser'])

//UserController class:
public function getUser(String $encoded_id)
{
    $user = User::findOrFail($encoded_id);
    ...
}

But fear not! This package extends Laravel's native route binding functionalities enabling you to use encoded ids in your parametric routes and still being able to inject your models in your Controller methods without having to manually querying the database inside of it, leaving room for the actual controller logic without cluttering it with unnecessary operations:

//Route file
Route::get('/users/{user:encoded_id}', [UserController::class, 'getUser'])

//UserController
public function getUser(User $user)
{
    ....
}

License

The MIT License (MIT). Please see License File for more information.

davide7h/encoded-ids 适用场景与选型建议

davide7h/encoded-ids 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 davide7h/encoded-ids 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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