In one of my Laravel projects, I ran into a challenge:
I needed to encode and decode strings efficiently. Initially, I turned to Laravel's built-in encrypt
and decrypt
functions. While they worked, the encoded strings were too long and unsuitable for my use case made the URLs messy and impractical for sharing..
Then I experimented with Base64 encoding. It was closer to what I needed, but making it URL-safe added unnecessary complexity. Frustrated by the lack of an all-in-one solution, I realized there wasn’t a straightforward package for encoding IDs and strings with a customizable and URL-safe approach.
So, I decided to create one.
What started as a Laravel-specific tool quickly evolved into a standalone package, capable of being used in any PHP project. This package offers:
- Simple and secure encoding/decoding mechanisms.
- Built-in support for URL-safe Base62 encoding.
- Customizability to add more encoding schemes, like Base58, Base64, or even your own.
- This package is perfect for creating URL-safe tokens, encoding database IDs, or any scenario where lightweight, secure encoding is required.
This package bridges the gap for developers who need a lightweight yet powerful solution for encoding and decoding strings and IDs across their projects.
Install the package using Composer:
composer require nassiry/laravel-encoder
Laravel Usage
use Nassiry\Encoder\Facades\Encoder;
// Encode and Decode IDs
$encodedId = Encoder::encodeId(12345, 4);
$decodedId = Encoder::decodeId($encodedId);
// Encode and Decode Strings
$encodedString = Encoder::encodeString('Hello World');
$decodedString = Encoder::decodeString($encodedString);
Standalone Usage
require __DIR__ . '/vendor/autoload.php';
use Nassiry\Encoder\Encoder;
// Create an encoder instance
$encoder = new Encoder();
// Encode an ID
$encodedId = $encoder->encodeId(12345, 4);
echo "Encoded ID: $encodedId\n"; // Example output: 9FNp
// Decode the encoded ID
$decodedId = $encoder->decodeId($encodedId);
echo "Decoded ID: $decodedId\n"; // Output: 12345
I hope this package helps simplify your encoding needs as much as it did for mine.
Feel free to share your feedback or contribute to the project on GitHub!
For more information and examples, refer to the GitHub repository.
Top comments (0)