PHP code example of dsentker / url-signature-bundle
1. Go to this page and download the library: Download dsentker/url-signature-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/ */
use Shift\UrlSignatureBundle\Utils\UrlSignatureBuilder;
class ExampleController extends AbstractController
{
/**
* @Route("/member/detail/{id}", name="member_detail")
*/
public function index(User $user, UrlSignatureBuilder $builder) {
// Just like the Twig function, the UrlSignatureBuilder offers in the third
// parameter to set an expiration date.
$hashedUrl = $builder->signUrlFromPath('example_path', ['param1' => 'value1'], '+10 minutes');
// You can also create a signature for a regular URL (without referring to a route path)
$hashedUrl = $builder->signUrl('https://example.com/foo', '+10 minutes');
}
use Shift\UrlSignatureBundle\Utils\RequestValidator;
class ExampleController extends AbstractController
{
/**
* @Route("/member/detail/{id}", name="member_detail")
*/
public function index(User $user, RequestValidator $signatureValidator) {
if(!$signatureValidator->isValid()) {
// is Signature missing or invalid? Show an alert, redirect or do something you like
}
// Alternatively, you can use this method. It throws an exception if the hash value
// is missing or not valid.
$signatureValidator->verify();
// There is no need to also inject the request object to your
// action method as it is provided by RequestValidator instance.
$request = $signatureValidator->getRequest();
}
use Shift\UrlSignatureBundle\Annotation\RequiresSignatureVerification;
class ExampleController extends AbstractController
{
/**
* @RequiresSignatureVerification()
*
* @Route("/member/detail/{id}", name="member_detail")
*/
public function index(User $user) {
// ...
}
}
use Shift\UrlSignatureBundle\Controller\UrlSignatureTrait;
class SingleActionController extends AbstractController
{
use UrlSignatureTrait;
/**
* @Route("/member/detail/{id}", name="member_detail")
*/
public function index(User $user) {
/** @var Shift\UrlSignatureBundle\Utils\UrlSignatureBuilder $builder */
$builder = $this->getBuilder();
/** @var Shift\UrlSignatureBundle\Utils\RequestValidator $validator */
$validator = $this->getValidator();
}
}