cayetanosoriano/hashids-bundle

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

安装数: 154,027

依赖项: 0

建议者: 0

安全: 0

星标: 23

关注者: 0

分支: 20

开放问题: 2

类型:symfony-bundle

v1.1 2016-12-02 19:52 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:23:40 UTC


README

SensioLabsInsight

hashidsBundle

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

安装

Symfony 2.1.x <= 2.4.x: Composer

Composer 是一个 PHP 项目的依赖关系管理器。您需要在 composer.json 文件中列出您的依赖项

{
    "require": {
        "cayetanosoriano/hashids-bundle": "dev-master"
    }
}

在实际项目中安装,请下载 composer 二进制文件并运行它

wget https://getcomposer.org.cn/composer.phar
# or
curl -O https://getcomposer.org.cn/composer.phar

php composer.phar install

步骤 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.