danilovl/hashids-bundle

Symfony bundle provides integrates hashids

Installs: 1 669

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

v1.7.1 2024-11-15 19:15 UTC

README

phpunit downloads latest Stable Version license

HashidsBundle

About

Symfony bundle provides integrates hashids.

Requirements

  • PHP 8.3 or higher
  • Symfony 7.0 or higher
  • Hashids 5.0 or higher

1. Installation

Install danilovl/hashids-bundle package by Composer:

composer require danilovl/hashids-bundle

Add the HashidsBundle to your application's bundles if does not add automatically:

<?php
// config/bundles.php

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

2. Usage

Project parameters.

# config/services.yaml

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

2.1 ParamConverter

HashidsBundle automatically provides decode hashids request parameters.

Routes.

# config/routes.yaml

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

In controllers requirement id will be decoded and ParamConverter will try to find Conversation by id.

<?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
        ]);
    }
}

Attribute if using MapEntity with specific keys in request.

#[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 Service

Get service HashidsService::class in controller.

<?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
        ]);
    }
}

Simple DI integration.

<?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 extension

Hashids encode filter in templates.

   <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 filter in templates.

   <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>

License

The HashidsBundle is open-sourced software licensed under the MIT license.