PHP code example of nzo / url-encryptor-bundle

1. Go to this page and download the library: Download nzo/url-encryptor-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nzo / url-encryptor-bundle example snippets


// config/bundles.php

return [
    // ...
    Nzo\UrlEncryptorBundle\NzoUrlEncryptorBundle::class => ['all' => true],
];

use Nzo\UrlEncryptorBundle\Annotations\ParamDecryptor;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class InvoiceController
{
    #[Route('/invoice/{id}', name: 'invoice_show')]
    #[ParamDecryptor(['id'])]
    public function show(string $id): Response
    {
        // $id is "42"
    }
}

use Nzo\UrlEncryptorBundle\Encryptor\Encryptor;
use Nzo\UrlEncryptorBundle\Exception\DecryptionException;

class InvoiceLinks
{
    public function __construct(private Encryptor $encryptor)
    {
    }

    public function example(): void
    {
        $encrypted = $this->encryptor->encrypt('42');

        try {
            $id = $this->encryptor->decrypt($encrypted);
        } catch (DecryptionException $e) {
            // not a value this application encrypted
        }
    }
}

#[ParamDecryptor(['id'], context: 'invoice')]
public function show(string $id): Response

$encrypted = $this->encryptor->encrypt($apiToken, 'user-' . $user->getId());

$this->encryptor->decrypt($encrypted, 'user-' . $user->getId()); // the token
$this->encryptor->decrypt($encrypted, 'user-' . $other->getId()); // DecryptionException

if ($this->encryptor->needsReencryption($stored)) {
    $stored = $this->encryptor->encrypt($this->encryptor->decrypt($stored));
}

php -r "echo base64_encode(random_bytes(32)), PHP_EOL;"