Download the PHP package dutymess/hashids without Composer
On this page you can find all versions of the php package dutymess/hashids. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package hashids
Hashids is a small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database numeric ids to users: https://hashids.org/php
This is a wrapper around the original hashids/hashids
package. For basic documentation refer to the original one.
https://github.com/vinkla/hashids
The above badges are for the original package.
Installation
Require this package, with Composer, in the root directory of your project.
Then you can enjoy the full features of the original package. Refer to their documentation for more details.
Herein, I just describe what has been added to it.
Advantages Over the Original Package
- It's configured for the most-appropriate usages, with alphabets and lengths already set out of the box.
- The encoder doesn't return an array for a single id. You don't have to worry about array/string conversions.
- Bypasser is a great tool if you face trouble.
- The model trait can be used on eloquent models and easily provide hashid tools.
- You can still enjoy all the benefits provided by the original package, without any overheads.
Basic Usage
The first thing you need to do is to define your salt in your .env
file.
Hashids must not be used as a means of security, but it is a good practice to redefine the salt per app.
Then, you are ready to convert ids and hashids to each other, through easy helper functions.
No matter what the input is, the hashid
function will always switch.
Use the below methods, if you want to be sure the returned data is of the desired type.
All the above methods receive arrays as well.
Model Helpers
As the most important usage of this package is to hide table id
s, we have introduced a special trait to be used in your model.
Then, the hashid
accessor is added to your model.
Moreover, a query scope is added to easily filter records by their hashids, instead of ids:
It's ok if you include real ids in the array. I.e. You don't have to ensure everything received is in the form of hashids. A mix of real ids and hashids work too.
:warning: Never save a hashid in your database. The key can be changed and you will lose your data.
Easy Bypass
Debugging the app when the hashids are engaged can be a real headache, as you have to constantly convert things in the tinker and see what is what.
To deal with this problem, you can bypass the mechanism in your .env
file.
This will bypass real encryption and just convert ids to an equivalent string to make you easily read the responses and follow the probable bugs.
:warning: Be sure to do this in the development mode only.
Custom Salts
You can define custom salts and freely use them in your app.
First, publish the config.
The fully-commented config file will be published and you can define as many salts as you may need.
The above helper functions can handle your custom salts by receiving a second argument.
Advanced Usage
Easy switching between ids and hashids is not what you may always want. While the above helper functions are designed to try their best to find the most appropriate values, you may want to have errors if the user submits id
instead of hashid
.
The below methods will not automatically convert things.
Moreover, you can have more control over the minimum length and the alphabets, using the below methods:
Note that passing null
as the alphabet will instruct the method to use the custom alphabet, which consists of English letters with no digits involved.
License
To respect the efforts made by the original maintainer, I have not touched any of the licenses.
Ivan Akimov
All versions of hashids with dependencies
ext-mbstring Version *