PHP code example of arthem / request-signer-bundle

1. Go to this page and download the library: Download arthem/request-signer-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/ */

    

arthem / request-signer-bundle example snippets



namespace App\Serializer\Normalizer;

use App\Entity\Asset;
use Arthem\RequestSignerBundle\RequestSigner;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

abstract class ApiNormalizer
{
    private UrlGeneratorInterface $urlGenerator;
    private RequestSigner $requestSigner;
    private RequestStack $requestStack;

    protected function generateAssetUrl(Asset $asset): string
    {
        return $this->requestSigner->signUri(
            $this->urlGenerator->generate('asset_preview', ['id' => $asset->getId()], UrlGeneratorInterface::ABSOLUTE_URL),
            $this->requestStack->getCurrentRequest(),
            [
                'signer' => 'aws_images', // override default adapter (optional)
                'ResponseContentDisposition' => 'attachment; filename=image.jpg', // Force S3 download
            ]
        );
    }
}


namespace App\Controller;

use Arthem\RequestSignerBundle\RequestSigner;
use Arthem\RequestSignerBundle\Exception\InvalidSignatureException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;

class AssetController
{
    /**
     * @Route("/assets/{id}", name="asset_preview")
     */
    public function previewAction(string $id, Request $request, RequestSigner $requestSigner)
    {
        try {
            $requestSigner->validateRequest($request);
        } catch (InvalidSignatureException $e) {
            throw new AccessDeniedHttpException($e->getMessage());
        }

        // Stream asset here
    }
}