HashidsBundle
Integrates hashids/hashids in a Symfony project.
Installation using composer
These commands requires you to have Composer installed globally.
Open a command console, enter your project directory and execute the following
commands to download the latest stable version of this bundle:
Using Symfony Flex
composer config extra.symfony.allow-contrib true
composer req roukmoute/hashids-bundle
Using Symfony 4 Framework
composer require roukmoute/hashids-bundle
If this has not been done automatically, enable the bundle by adding the
following line in the config/bundles.php
file of your project:
<?php
return [
…,
Roukmoute\HashidsBundle\RoukmouteHashidsBundle::class => ['all' => true],
];
Configuration
The configuration (config/packages/roukmoute_hashids.yaml
) looks as follows :
roukmoute_hashids:
# if set, the hashids will differ from everyone else's
salt: ""
# if set, will generate minimum length for the id
# 0 — meaning hashes will be the shortest possible length
min_hash_length: 0
# if set, will use only characters of alphabet string
alphabet: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
# if set to true, it will continue with the next available param converters
passthrough: false
Usage
$hashids = $this->get('hashids');
Next it's the same things of official documentation.
Other Features
The Roukmoute\HashidsBundle\Hashids
has extra features:
$minHashLength = 42;
// Edit the minimum hash length.
$this->get('hashids')->setMinHashLength($minHashLength)->encode(1, 2, 3);
// Encode with a custom minimum hash length.
$this->get('hashids')->encodeWithCustomHashLength($minHashLength, 1, 2, 3);
Hashids Converter
Converter Name: hashids.converter
The hashids converter attempts to convert hashid
/id
attribute set in the route into an integer parameter.
You could use hashid
or id
to add :
/**
* @Route("/users/{hashid}")
*/
public function getAction(int $user)
{
}
or
/**
* @Route("/users/{id}")
*/
public function getAction(int $user)
{
}
For specific case, just add "hashid" = "{parameter_name}"
in ParamConverter
options:
/**
* @Route("/users/{slug}")
*
* @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"hashid" = "user"})
*/
public function getAction(int $user)
{
}
You could have several hashids one the same URL:
/**
* @Route("/users/{user}/status/{status}")
*
* @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"hashid" = "user"})
* @ParamConverter("status", class="RoukmouteBundle\Entity\Notification", options={"hashid" = "status"})
*/
public function getAction(int $user, int $status)
{
}
Using Passthrough
Passthrough
allows to continue with the next available param converters.
So if you would like to retrieve an object instead of an integer, just active
passthrough :
roukmoute_hashids:
passthrough: true
Base on the example above:
/**
* @Route("/users/{hashid}")
*/
public function getAction(User $user)
{
}
As you can see, the passthrough feature allows to use DoctrineParamConverter
or any another ParamConverter
you would have created.
Twig Extension
Usage
{{ path('users.show', {'hashid': user.id | hashids_encode }) }}
{{ app.request.query.get('hashid') | hashids_decode }}
Copyright (c) 2016 Mathias STRASSER
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-
Add Symfony 5 compatibility
By roukmoute, 1 year ago
-
Switch from \twig_extension to AbstractExtension (#11)
By roukmoute, 1 year ago
-
Fixed configuration loading, deprecated since Symfony 4.2. (#9)
By roukmoute, 1 year ago
-
Fixed getIdentifier() must be of the type string (#7)
By roukmoute, 1 year ago
-
Use Hashids\HashidsInterface as alias to hashids service in order to allow autowiring
By roukmoute, 1 year ago
-
Implements v2.0.0 (#5)
By web-flow, 2 years ago
-
Make `hashids` service public
By Mathias STRASSER, 2 years ago
-
Loosened symfony/http--kernel version restriction (#4)
By roukmoute, 2 years ago
-
Fix issues about doctrine dependencies
By Mathias STRASSER, 2 years ago
-
Add "doctrine/orm" dependency in require-dev
By Mathias STRASSER, 2 years ago
-
Add "doctrine" service
By Mathias STRASSER, 3 years ago
-
Add missing dependency symfony/http-kernel
By Mathias STRASSER, 3 years ago
-
Remove useless getName() method
By Mathias STRASSER, 3 years ago
-
Refactor getHashIdentifier() method
By Mathias STRASSER, 3 years ago
-
Add tests for Hashids
By Mathias STRASSER, 3 years ago
-
Add tests for HashidsExtension
By Mathias STRASSER, 3 years ago
-
Add tests for HashidsParamConverter
By Mathias STRASSER, 3 years ago
-
Add phpspec/phpspec dependency
By Mathias STRASSER, 3 years ago
-
Fix composer's autoload fault
By Mathias STRASSER, 3 years ago
-
Depend missing sensio/framework-extra-bundle
By Mathias STRASSER, 3 years ago
-
Add config section
By Mathias STRASSER, 3 years ago
-
Bump branch-alias dev-master to 1.5-dev
By Mathias STRASSER, 3 years ago
-
Add decodeWithCustomHashLength method
By Mathias STRASSER, 3 years ago
-
Add Scrutinizer badge
By web-flow, 3 years ago
-
Add encode / decode methods in twig template
By roukmoute, 3 years ago
-
Bump branch-alias dev-master to 1.4-dev
By web-flow, 4 years ago
-
Replace \LogicException with NotFoundHttpException
By web-flow, 4 years ago
-
Remove flag @dev for hashids/hashids
By web-flow, 4 years ago
-
Remove final class
By web-flow, 4 years ago
-
Add autowiring
By roukmoute, 4 years ago