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:
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
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);
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.
The above is the detailed content of Why I Built the Laravel Encoding Package I Couldn't Find Anywhere Else. For more information, please follow other related articles on the PHP Chinese website!