uniacid/hashid-bundle
Composer 安装命令:
composer require uniacid/hashid-bundle
包简介
Modernized Symfony bundle for automatic encoding/decoding of route parameters using Hashids - PHP 8.3 & Symfony 6.4/7.0 ready fork of pgs-soft/hashid-bundle
README 文档
README
A modernized fork of PGSSoft/HashId bundle, updated for PHP 8.3 and Symfony 6.4/7.0
Symfony bundle for automatically encoding integer route parameters and decoding request parameters using Hashids
🚀 Version 4.0 Features
- PHP 8.1+ Attributes: Native
#[Hash]attributes replace annotations - Modern PHP Support: PHP 8.1, 8.2, and 8.3 features including readonly properties, typed constants
- Symfony 6.4 LTS & 7.0: Full compatibility with latest Symfony versions
- 75.3% Rector Automation: Automated migration from v3.x using Rector
- PHPStan Level 9: Enhanced type safety and code quality
- Multiple Hasher Support: Configure different hashers with unique settings
Why Use HashId?
Transform predictable integer URL parameters into obfuscated strings automatically:
/order/315→/order/4w9aA11avM/user/1337→/user/X46dBNxd79/hash-id/demo/decode/216/30→/hash-id/demo/decode/X46dBNxd79/ePOwvANg
Benefits
- Security: Prevent resource enumeration attacks
- Transparency: No code changes needed - works with existing
generateUrl()and{{ url() }} - Compatibility: Full Doctrine ParamConverter support
- Flexibility: Configure salt, minimum length, and alphabet
Installation
composer require uniacid/hashid-bundle:^4.0
Requirements
- PHP 8.1 or higher (8.3 recommended)
- Symfony 6.4 LTS or 7.0
- hashids/hashids ^4.0 or ^5.0
Configuration
# config/packages/pgs_hash_id.yaml pgs_hash_id: salt: '%env(HASHID_SALT)%' min_hash_length: 10 alphabet: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' # v4.0 Compatibility Settings (optional) compatibility: suppress_deprecations: false # Set to true during migration prefer_attributes: true # Use attributes when both are present
Usage
Modern Usage with PHP 8.1+ Attributes (Recommended)
<?php namespace App\Controller; use Pgs\HashIdBundle\Attribute\Hash; use Symfony\Component\Routing\Attribute\Route; class OrderController { #[Route('/order/{id}')] #[Hash('id')] public function show(int $id): Response { // $id is automatically decoded from hash to integer // Example: URL /order/4w9aA11avM → $id = 315 } #[Route('/compare/{id}/{otherId}')] #[Hash(['id', 'otherId'])] public function compare(int $id, int $otherId): Response { // Multiple parameters can be hashed } }
Legacy Annotation Support (Deprecated, will be removed in v5.0)
use Pgs\HashIdBundle\Annotation\Hash; use Symfony\Component\Routing\Annotation\Route; class LegacyController { /** * @Route("/user/{id}") * @Hash("id") */ public function edit(int $id): Response { // Still works in v4.0 for backward compatibility } }
Twig Templates
No changes needed! The bundle works transparently:
{# Automatically encodes the id parameter #} <a href="{{ url('order_show', {'id': order.id}) }}">View Order</a> {# Generates: /order/4w9aA11avM #}
Controllers and Services
// Automatic encoding in controllers return $this->redirectToRoute('order_show', ['id' => $orderId]); // Automatic encoding in services $url = $this->router->generate('order_show', ['id' => $orderId]);
Migration from v3.x
Automated Migration with Rector (75.3% Automation)
# Install Rector if not present composer require rector/rector --dev # Run automated migration (dry-run first) vendor/bin/rector process --config=rector.php --dry-run # Apply changes vendor/bin/rector process --config=rector.php
Key Changes in v4.0
- Minimum PHP: 7.2 → 8.1
- Symfony: 4.4/5.x → 6.4/7.0
- Annotations: Deprecated in favor of attributes
- PHPStan: Level 4 → Level 9
- Type Coverage: 65% → 95%
For detailed migration instructions, see UPGRADE-4.0.md.
Advanced Features
Multiple Hasher Configuration
# config/packages/pgs_hash_id.yaml pgs_hash_id: # Default hasher configuration salt: '%env(HASHID_SALT)%' min_hash_length: 10 hashers: secure: salt: '%env(SECURE_HASHID_SALT)%' min_hash_length: 20 public: salt: 'public-content' min_hash_length: 8
Doctrine ParamConverter Integration
Works seamlessly with Doctrine entities:
#[Route('/order/{id}/invoice')] #[Hash('id')] public function invoice(#[MapEntity] Order $order): Response { // Doctrine automatically loads the Order entity // using the decoded integer ID }
Testing
Run the test suite:
# Run all tests vendor/bin/phpunit # Run with coverage vendor/bin/phpunit --coverage-html coverage/ # Run documentation validation tests vendor/bin/phpunit tests/Documentation/
Documentation
- Installation Guide
- Configuration Reference
- Migration Guide
- Rector Metrics
- API Documentation
- Security Considerations
- Performance Guide
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/uniacid/HashId.
See CONTRIBUTING.md for development setup and guidelines.
About
Original Work
This bundle is a fork of the excellent HashId Bundle originally created by PGS Software.
This Fork
This modernized fork is maintained by uniacid and focuses on:
- PHP 8.3 compatibility and modern features
- Symfony 6.4 LTS and 7.0 support
- Automated migration tools with Rector
- Enhanced performance and type safety
Version 4.0 Modernization
- Modernization Lead: AI-Assisted Development Team
- Rector Automation: 75.3% automation rate achieved
- Testing: PHPUnit 10 migration with 90%+ coverage
- Documentation: Comprehensive upgrade guides and API documentation
License
This bundle is released under the MIT license. See the LICENSE file for details.
Follow us
uniacid/hashid-bundle 适用场景与选型建议
uniacid/hashid-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「routing」 「security」 「url」 「bundle」 「parameter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 uniacid/hashid-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 uniacid/hashid-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 uniacid/hashid-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
It's a barebone security class written on PHP
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
Bundle Symfony DaplosBundle
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-17