paramguard / url-encoder
Laravel package for URL parameter encoding
Requires
- php: >=8.0
- illuminate/support: ^8.0|^9.0|^10.0
README
Secure URL parameter encryption for Laravel applications. Protect sensitive data in your routes with AES-128-CTR encryption.
Features
- Automatic encryption/decryption of route parameters
- Seamless integration with Laravel's URL generation
- AES-128-CTR encryption with random IVs
- URL-safe base64 encoding
- Configurable route groups and exclusions
- JavaScript compatibility
- Optional full source publish for local development
Installation
Install via Composer:
composer require paramguard/url-encoder
Publish the config file:
php artisan vendor:publish --provider="ParamGuard\UrlEncoder\UrlEncoderServiceProvider" --tag="url-encoder-config"
Configuration
Set these in your .env
:
URL_ENCODE_ENABLE=true URL_ENCRYPTION_SECRET_KEY=your-32-character-secret-key-here
Edit config/url-encoder.php
to customize:
<?php return [ /* |-------------------------------------------------------------------------- | Middleware Alias |-------------------------------------------------------------------------- */ 'middleware_alias' => 'url-encode', /* |-------------------------------------------------------------------------- | Enabled Route Groups |-------------------------------------------------------------------------- */ 'enable_route_groups' => [ 'web' ], /* |-------------------------------------------------------------------------- | Excluded Routes |-------------------------------------------------------------------------- */ 'exclude_routes' => [ 'login', 'register' ], /* |-------------------------------------------------------------------------- | Enable Encoding |-------------------------------------------------------------------------- */ 'is_encoding_enable' => env('URL_ENCODE_ENABLE', false), /* |-------------------------------------------------------------------------- | Encryption Secret Key |-------------------------------------------------------------------------- */ 'url_encryption_secret_key' => env('URL_ENCRYPTION_SECRET_KEY'), ];
Usage
Config-cache Command:
php artisan config:cache
Basic Usage
Route::get('/project/{project_id}/task/{task_id}', function ($project_id, $task_id) { // Parameters are automatically decrypted })->name('project.task.show');
JavaScript Integration
PHP Side (Controller/Blade):
$urlData = encryptedRoute('project.task.show', ['project_id', 'task_id']);
JavaScript Side:
<script> let action_url = @json($urlData['url']) .replace(@json($urlData['placeholders']['project_id']), projectId) .replace(@json($urlData['placeholders']['task_id']), taskId); fetch(action_url) .then(response => response.json()) .then(data => console.log(data)); </script>
Methods
encryptedRouteJS(string $routeName, array $paramNames)
$urlData = encryptedRouteJS('route.name', ['param1', 'param2']); // Returns: ['url' => '...', 'placeholders' => [...]]
Local Development (Full Source Access)
If a developer needs to modify the package code locally, the package provides an optional source publish:
In UrlEncoderServiceProvider
:
$this->publishes([ __DIR__ . '/../' => base_path('packages/paramguard/url-encoder'), ], 'url-encoder-source');
Publish the source:
php artisan vendor:publish --tag=url-encoder-source
This will copy the full package into:
packages/paramguard/url-encoder
To use the local version, update your composer.json
with the following repository entry:
"repositories": [ { "type": "path", "url": "packages/paramguard/url-encoder" } ]
Then run:
composer update paramguard/url-encoder
This will local package so changes take effect immediately without needing to reinstall.
Local Development (From Git Clone)
If you prefer to clone the package repo directly and develop locally with a symlink:
Clone the package repo outside your Laravel project folder:
git clone https://github.com/ataulgalib/laravel-url-encoder.git ../laravel-url-encoder
Add this repository to your Laravel project’s composer.json:
"repositories": [ { "type": "path", "url": "../laravel-url-encoder", "options": { "symlink": true } } ]
Require the package locally using the dev version:
composer require paramguard/url-encoder:@dev
Publish the package config:
php artisan vendor:publish --provider="ParamGuard\UrlEncoder\UrlEncoderServiceProvider" --tag="url-encoder-config"
This symlinks the package source from your cloned repo into your Laravel project, so any changes in the clone take effect immediately.
Contributing
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature
- Make changes and commit:
git commit -m "Description of change"
- Push to your fork:
git push origin feature/your-feature
- Submit a Pull Request.
We’ll review your contribution together before merging.
License
This package is open-source and licensed under the MIT License.