hbryan/hashids-bundle

将 hashids 库集成到容器中的组件

安装: 73

依赖者: 0

建议者: 0

安全性: 0

星级: 0

关注者: 2

分支: 20

类型:symfony-bundle

v2.2 2019-05-06 11:03 UTC

This package is not auto-updated.

Last update: 2024-10-01 13:31:21 UTC


README

SensioLabsInsight

hashidsBundle

这是一个将 http://www.hashids.org/ 作为服务使用的组件

安装

composer require hbryan/hashids-bundle

步骤 2:启用组件

最后,在内核中启用组件

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...

            new cayetanosoriano\HashidsBundle\cayetanosorianoHashidsBundle(),
    );
}

配置

将以下内容添加到您的 config.yml 中

cayetanosoriano_hashids:
    salt: "randomsalt" #optional
    min_hash_length: 10 #optional
    alphabet: "abcd..." #optional

然后使用该服务

$kcy = $this->get('hashids');

可选功能

Doctrine 参数转换器

包含的 Doctrine 参数转换器扩展了 (SensioFrameworkExtraBundle)[https://symfony.com.cn/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html],以在获取对象之前自动解码路由中的 Hashids。

在你的 services.yml 中覆盖默认服务

sensio_framework_extra.converter.doctrine.orm:
    class: cayetanosoriano\HashidsBundle\Request\ParamConverter\HashidsDoctrineParamConverter
    arguments: ["@hashids", "@doctrine"]
    tags: [{ name: request.param_converter, converter: doctrine.orm }]

在您的路由中指定 Hashid

以下两个示例是等价的,可以使用原始数据库 id 或 Hashid 编码版本。

原始 id(SensioFrameworkExtraBundle 标准行为)
/**
 * @Route("/user/{id}", requirements={"id"="\d+"}, name="user_view")
 */
public function viewAction(User $user)
{
…
}
Hashid

hashid 请求参数将自动识别为 id 的编码版本。

/**
 * @Route("/user/{hashid}", requirements={"hashid"="[A-Za-z0-9_-]+"}, name="user_view")
 */
public function viewAction(User $user)
{
…
}

许可证

=======

Twig 扩展

在 services.yml 中声明该服务

twig.hashids_extension:
    class: cayetanosoriano\HashidsBundle\Twig\HashidsExtension
    arguments: ["@hashids"]
    public: false
    tags: [{ name: twig.extension }]

在您的 Twig 模板中使用该扩展

<a href="{{ path('user_profile', {'hashid': user.id|hashid_encode}) }}">View Profile</a>

许可证

Copyright (c) 2015 neoshadybeat[at]gmail.com

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.