philetaylor/hashids-bundle

用于将hashids库集成到容器中的工具包

安装数量: 7,036

依赖者: 0

推荐者: 0

安全性: 0

星标: 1

关注者: 3

分支: 20

类型:symfony-bundle

4.0.2 2022-09-21 14:59 UTC

This package is auto-updated.

Last update: 2024-09-21 19:16:51 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)中包含的转换器,以在获取对象之前自动解码路由中的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.