Skip to content

Commit 33385d5

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: [HttpKernel] Document UriSigner
2 parents 3996093 + 7bd7a9d commit 33385d5

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

routing.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,49 @@ defined as annotations:
26462646
:doc:`another way to enforce HTTP or HTTPS </security/force_https>`
26472647
via the ``requires_channel`` setting.
26482648

2649+
Signing URIs
2650+
~~~~~~~~~~~~
2651+
2652+
A signed URI is an URI that includes a hash value that depends on the contents of
2653+
the URI. This way, you can later check the integrity of the signed URI by
2654+
recomputing its hash value and comparing it with the hash included in the URI.
2655+
2656+
Symfony provides a utility to sign URIs via the :class:`Symfony\\Component\\HttpKernel\\UriSigner`
2657+
service, which you can inject in your services or controllers::
2658+
2659+
// src/Service/SomeService.php
2660+
namespace App\Service;
2661+
2662+
use Symfony\Component\HttpKernel\UriSigner;
2663+
2664+
class SomeService
2665+
{
2666+
public function __construct(
2667+
private UriSigner $uriSigner,
2668+
) {
2669+
}
2670+
2671+
public function someMethod()
2672+
{
2673+
// ...
2674+
2675+
// generate a URL youself or get it somehow...
2676+
$url = 'https://example.com/foo/bar?sort=desc';
2677+
2678+
// sign the URL (it adds a query parameter called '_hash')
2679+
$signedUrl = $this->uriSigner->sign($url);
2680+
// $url = 'https://example.com/foo/bar?sort=desc&_hash=e4a21b9'
2681+
2682+
// check the URL signature
2683+
$uriSignatureIsValid = $this->uriSigner->check($signedUrl);
2684+
// $uriSignatureIsValid = true
2685+
2686+
// if you have access to the current Request object, you can use this
2687+
// other method to pass the entire Request object instead of the URI:
2688+
$uriSignatureIsValid = $this->uriSigner->checkRequest($request);
2689+
}
2690+
}
2691+
26492692
Troubleshooting
26502693
---------------
26512694

0 commit comments

Comments
 (0)