vipinbose/hashids-bundle

在 Symfony 项目中集成 hashids/hashids

安装: 54

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 0

分支: 14

类型:symfony-bundle

v1.0.1 2024-06-06 11:39 UTC

This package is auto-updated.

Last update: 2024-09-18 12:26:03 UTC


README

在 Symfony 项目中集成 hashids/hashids

使用 composer 安装

以下命令要求您已全局安装 Composer。
打开命令行控制台,进入您的项目目录,并执行以下命令以下载此扩展的最新稳定版本

使用 Symfony Flex

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

仅使用 Symfony 框架

    composer require vipinbose/hashids-bundle

如果此操作未自动完成,请通过在您的项目 config/bundles.php 文件中添加以下行来启用扩展

<?php

return [
    …,
    Vipinbose\HashidsBundle\VipinboseHashidsBundle::class => ['all' => true],
];

配置

配置文件(config/packages/vipinbose_hashids.yaml)如下所示

vipinbose_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 tries to convert all arguments passed to the controller
    auto_convert:    true

用法

use Vipinbose\HashidsBundle\Interfaces\HashidsServiceInterface;

public function __construct(
        private HashidsServiceInterface $hasher,
    ) {
    }

接下来是官方文档中相同的内容。官方文档

Hashids 转换器

转换器名称: hashids.converter

hashids 转换器尝试将路由中设置的任何属性集转换为整数参数。

您应该使用 hashid

/**
 * @Route("/users/{hashid}")
 */
public function getAction(User $user)
{
}

使用 auto_convert

auto_convert 尝试将控制器中的所有参数转换为整数。

vipinbose_hashids:
  auto_convert: true

Twig 扩展

用法

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