danilovl/hashids-bundle

Symfony 扩展包,用于集成 hashids

安装次数: 1,645

依赖关系: 0

建议者: 0

安全性: 0

星级: 0

关注者: 1

分支: 0

公开问题: 0

类型:symfony-bundle

v1.6.2 2024-03-30 07:50 UTC

README

phpunit downloads latest Stable Version license

HashidsBundle

关于

Symfony 扩展包,提供 hashids 的集成。

要求

  • PHP 8.3 或更高版本
  • Symfony 7.0 或更高版本
  • Hashids 5.0 或更高版本

1. 安装

使用 Composer 安装 danilovl/hashids-bundle

composer require danilovl/hashids-bundle

如果未自动添加,将 HashidsBundle 添加到您的应用程序的包中

<?php
// config/bundles.php

return [
    // ...
    Danilovl\HashidsBundle\HashidsBundle::class => ['all' => true]
];

2. 使用

项目参数。

# config/services.yaml

danilovl_hashids:
  salt: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
  min_hash_length: 20
  alphabet: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
  enable_param_converter: false 

2.1 参数转换器

HashidsBundle 自动提供解码 hashids 请求参数。

路由。

# config/routes.yaml

conversation_detail:
  path: /detail/{id}
  requirements:
    id: '^[a-zA-Z0-9]{10}$'
  defaults:
    _controller: App\Controller\ConversationController:detail
  methods: [GET, POST]

在控制器要求中,id 将被解码,ParamConverter 将尝试通过 id 找到 Conversation

<?php declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;

class ConversationController extends AbstractController
{
    public function detail(Request $request, Conversation $conversation): Response
    {
        return $this->render('conversation/detail.html.twig', [
            'conversation' => $conversation
        ]);
    }
}

如果使用 MapEntity 并在请求中指定特定键,则可以使用属性。

#[HashidsRequestConverterAttribute(requestAttributesKeys: ['id_work', 'id_task'])]
public function edit(
    Request $request,
    #[MapEntity(mapping: ['id_work' => 'id'])] Work $work,
    #[MapEntity(mapping: ['id_task' => 'id'])] Task $task
): Response {
    $this->denyAccessUnlessGranted(VoterSupportConstant::EDIT, $task);

    return $this->taskEditHandle->handle($request, $work, $task);
}

2.2 服务

在控制器中获取服务 HashidsService::class

<?php declare(strict_types=1);

namespace App\Controller;

use Danilovl\HashidsBundle\Service\HashidsService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;

class UserController extends AbstractController
{
    public function detail(Request $request): Response
    {
        $userId = $this->get(HashidsService::class)->decode($request->get('id'));
        if ($userId) {
            $userId = $this->get(HashidsService::class)->encode($request->get('id'));
        }

        return $this->render('profile/edit.html.twig', [
            'userId' => $userId
        ]);
    }
}

简单的依赖注入集成。

<?php declare(strict_types=1);

namespace App\Controller;

use Danilovl\HashidsBundle\Interfaces\HashidsServiceInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class UserController extends AbstractController
{
    public function __construct(private HashidsServiceInterface $hashidsService)
    {
    }

    public function detail(Request $request): Response
    {
        $userId = $this->hashidsService->decode($request->get('id'));
        if ($userId) {
            $userId = $this->hashidsService->encode($request->get('id'));
        }

        return $this->render('profile/edit.html.twig', [
            'userId' => $userId
        ]);
    }
}

2.3 Twig 扩展

在模板中使用 Hashids encode 过滤器。

   <a target="_blank"
      href="{{ path('user_detail', { 'id': user.id | hashids_encode }) }}"
      class="btn btn-primary btn-xs">
       <i class="fa fa-desktop"></i>
       {{ 'app.form.action.show_detail' | trans() }}
   </a>

在模板中使用 Hashids decode 过滤器。

   <a target="_blank"
      href="{{ path('user_detail', { 'id': user.id | hashids_decode }) }}"
      class="btn btn-primary btn-xs">
       <i class="fa fa-desktop"></i>
       {{ 'app.form.action.show_detail' | trans() }}
   </a>

许可证

HashidsBundle 是开源软件,许可协议为 MIT 许可证