Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Hashids Bundle Laravel Package

danilovl/hashids-bundle

Symfony bundle integrating Hashids for encoding/decoding IDs. Configure salt, alphabet, and minimum hash length. Optional ParamConverter/Request converter automatically decodes route/request parameters so controllers can receive entities by decoded IDs.

View on GitHub
Deep Wiki
Context7

phpunit downloads latest Stable Version license

HashidsBundle

About

Symfony bundle provides integrates hashids.

Requirements

  • PHP 8.5 or higher
  • Symfony 8.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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle