roukmoute/hashids-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

roukmoute/hashids-bundle

Composer 安装命令:

composer require roukmoute/hashids-bundle

包简介

Integrates hashids/hashids in a Symfony project

README 文档

README

SymfonyInsight Scrutinizer Code Quality Packagist Downloads

HashidsBundle

Note: This bundle is maintained, but for new projects, consider using roukmoute/sqids-bundle instead. Sqids is the official successor to Hashids, featuring a simpler algorithm, consistent cross-language output, and a built-in profanity blocklist. However, Sqids is not a drop-in replacement — it produces different IDs. Only migrate if you don't rely on previously generated Hashids. See the official Hashids recommendation for more details.

Integrates hashids/hashids in a Symfony project.

Installation using composer

These commands requires you to have Composer installed globally.
Open a command console, enter your project directory and execute the following commands to download the latest stable version of this bundle:

Using Symfony Flex

    composer config extra.symfony.allow-contrib true
    composer req roukmoute/hashids-bundle

Using Symfony Framework only

    composer require roukmoute/hashids-bundle

If this has not been done automatically, enable the bundle by adding the following line in the config/bundles.php file of your project:

<?php

return [
    …,
    Roukmoute\HashidsBundle\RoukmouteHashidsBundle::class => ['all' => true],
];

Configuration

The configuration (config/packages/roukmoute_hashids.yaml) looks as follows :

roukmoute_hashids:

    # if set, the hashids will differ from everyone else's
    salt:            ""

    # if set, will generate minimum length for the id
    # 0 — meaning hashes will be the shortest possible length
    min_hash_length: 0

    # if set, will use only characters of alphabet string
    alphabet:        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

    # if set to true, it will continue with the next available value resolvers
    passthrough:     false

    # if set to true, it tries to convert all arguments passed to the controller
    auto_convert:    false

Usage

use Hashids\HashidsInterface;

public function postShow(HashidsInterface $hashids): Response
{
    $hashids->
}

Next it's the same things of official documentation.

Hashids Value Resolver

The hashids value resolver attempts to convert any attribute set in the route into an integer parameter.

Using the #[Hashid] attribute

The recommended way is to use the #[Hashid] attribute on your controller parameter:

use Roukmoute\HashidsBundle\Attribute\Hashid;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/posts/{id}')]
public function show(#[Hashid] int $id): Response
{
    // $id is automatically decoded from the hashid
}

You can also specify a different route parameter name using the parameter option:

#[Route('/posts/{hash}')]
public function show(#[Hashid(parameter: 'hash')] int $id): Response
{
    // The 'hash' route parameter is decoded into $id
}

Using route aliases

You can also use hashid or id as route parameter names:

#[Route('/users/{hashid}')]
public function getAction(int $user): Response
{
}

or

#[Route('/users/{id}')]
public function getAction(int $user): Response
{
}

Using the _hash_ prefix

You can have several hashids in the same URL using the _hash_ prefix:

#[Route('/users/{_hash_user}/status/{_hash_status}')]
public function getAction(int $user, int $status): Response
{
}

The keys must match the controller parameter names:

//                          _hash_user  _hash_status
//                                 ↕            ↕
public function getAction(int $user, int $status)

You will receive a LogicException if an explicit hash could not be decoded correctly.

Using auto_convert

auto_convert tries to convert all arguments in controller.

roukmoute_hashids:
  auto_convert: true

Based on the example above:

#[Route('/users/{user}/status/{status}')]
public function getAction(int $user, int $status): Response
{
}

It will not be possible to get an exception of type LogicException from the bundle if it is activated.

Using passthrough

passthrough allows to continue with the next available value resolvers. So if you would like to retrieve an object instead of an integer, just activate passthrough:

roukmoute_hashids:
    passthrough: true

Based on the example above:

#[Route('/users/{hashid}')]
public function getAction(User $user): Response
{
}

As you can see, the passthrough feature allows to use EntityValueResolver or any other ValueResolverInterface you would have created.

Twig Extension

Usage

{{ path('users.show', {'hashid': user.id | hashids_encode }) }}
{{ app.request.query.get('hashid') | hashids_decode }}

roukmoute/hashids-bundle 适用场景与选型建议

roukmoute/hashids-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 332.74k 次下载、GitHub Stars 达 46, 最近一次更新时间为 2016 年 06 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 roukmoute/hashids-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 332.74k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 46
  • 点击次数: 10
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 46
  • Watchers: 2
  • Forks: 16
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-14