aaugustyniak/params-codec-bundle
Symfony 2/3 bundle that AES-encrypts route parameters. Adds a param_codec service and Twig helpers to generate encrypted URLs, plus a @DecryptParams annotation to automatically decrypt controller arguments using a secret passphrase.
Installation:
composer require aaugustyniak/params-codec-bundle
Add the bundle to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
Aaugustyniak\ParamsCodecBundle\ParamsCodecBundle::class => ['all' => true],
Configuration:
Add a secret passphrase to config/packages/params_codec.yaml (Symfony 4+):
params_codec:
secret: '%env(APP_SECRET)%' # Must match your .env
For Symfony 2/3, use parameters.yml:
params_codec:
secret: '%secret%'
First Use Case: Encrypt a parameter in a route and decrypt it in the controller:
<!-- Twig template -->
<a href="{{ path('route_name', { 'param': 'sensitive_data' }) }}">Link</a>
The URL will auto-encode the parameter. Decrypt it in the controller:
use Aaugustyniak\ParamsCodecBundle\Annotations\DecryptParams;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class SecureController extends Controller
{
/**
* @Route("/secure/{param}", name="route_name")
* @DecryptParams({"param": "param"})
*/
public function secureAction($param)
{
// $param is now decrypted
}
}
Route Parameter Encryption:
path() or url() functions to generate links with encrypted params:
{{ path('user_profile', { id: user.id, token: user.token }) }}
{param}).Controller Decryption:
@DecryptParams to auto-decrypt params:
/**
* @Route("/profile/{userId}/{token}", name="profile")
* @DecryptParams({"userId": "userId", "token": "token"})
*/
public function showProfile($userId, $token) { ... }
Custom Codecs:
Aaugustyniak\ParamsCodecBundle\Codec\ParamCodecInterface for custom logic (e.g., Base64, custom hashing):
class CustomCodec implements ParamCodecInterface {
public function encode($data) { ... }
public function decode($data) { ... }
}
param_codec.codec.Dynamic Routes:
requirements:
# config/routes.yaml
secure_route:
path: /secure/{encodedParam}
controller: App\Controller\SecureController::index
requirements:
encodedParam: ".+" # Accept any encoded string
Twig Extensions:
param_codec_encode() and param_codec_decode() in Twig for manual operations:
{% set encoded = param_codec_encode('secret') %}
{% set decoded = param_codec_decode(encoded) %}
ParamCodec service in tests:
$this->container->set('param_codec', $this->createMock(ParamCodecInterface::class));
Secret Management:
parameters.yml. Use environment variables (e.g., .env) and Symfony’s %env().Annotation Conflicts:
@DecryptParams may conflict with other annotations (e.g., @ParamConverter). Ensure proper ordering in route loading.URL Length Limits:
Twig Auto-escaping:
|raw filter if needed:
{{ path('route', { param: 'data' })|raw }}
Symfony 4+ Configuration:
config/packages/params_codec.yaml. If missing, create it or adjust autoloading.Decryption Failures:
Aaugustyniak\ParamsCodecBundle\Exception\DecodeException in logs. Common causes:
@DecryptParams.Route Mismatches:
{param}) match the keys in @DecryptParams.Service Not Found:
param_codec service is registered:
php bin/console debug:container param_codec
Custom Codec Logic:
AesCodec or implement ParamCodecInterface for non-AES encryption (e.g., RSA, Fernet).Event Listeners:
kernel.request to pre-process decryption or log events:
// src/EventListener/CodecListener.php
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
if ($request->attributes->has('encoded_param')) {
$decoded = $this->paramCodec->decode($request->attributes->get('encoded_param'));
$request->attributes->set('decoded_param', $decoded);
}
}
Twig Filters:
$twig->addFilter(new \Twig_SimpleFilter('custom_encode', [$this->paramCodec, 'encode']));
Batch Processing:
ParamCodec service directly:
$encoder = $this->container->get('param_codec');
$encoded = $encoder->encode($plaintext);
How can I help you explore Laravel packages today?