Skip to content

Commit 7bd7a9d

Browse files
committed
minor #18518 [HttpKernel] Document UriSigner (javiereguiluz)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [HttpKernel] Document UriSigner Fixes #18330. Commits ------- 93699b6 [HttpKernel] Document UriSigner
2 parents c2c22eb + 93699b6 commit 7bd7a9d

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
@@ -2617,6 +2617,49 @@ defined as annotations:
26172617
:doc:`another way to enforce HTTP or HTTPS </security/force_https>`
26182618
via the ``requires_channel`` setting.
26192619

2620+
Signing URIs
2621+
~~~~~~~~~~~~
2622+
2623+
A signed URI is an URI that includes a hash value that depends on the contents of
2624+
the URI. This way, you can later check the integrity of the signed URI by
2625+
recomputing its hash value and comparing it with the hash included in the URI.
2626+
2627+
Symfony provides a utility to sign URIs via the :class:`Symfony\\Component\\HttpKernel\\UriSigner`
2628+
service, which you can inject in your services or controllers::
2629+
2630+
// src/Service/SomeService.php
2631+
namespace App\Service;
2632+
2633+
use Symfony\Component\HttpKernel\UriSigner;
2634+
2635+
class SomeService
2636+
{
2637+
public function __construct(
2638+
private UriSigner $uriSigner,
2639+
) {
2640+
}
2641+
2642+
public function someMethod()
2643+
{
2644+
// ...
2645+
2646+
// generate a URL youself or get it somehow...
2647+
$url = 'https://example.com/foo/bar?sort=desc';
2648+
2649+
// sign the URL (it adds a query parameter called '_hash')
2650+
$signedUrl = $this->uriSigner->sign($url);
2651+
// $url = 'https://example.com/foo/bar?sort=desc&_hash=e4a21b9'
2652+
2653+
// check the URL signature
2654+
$uriSignatureIsValid = $this->uriSigner->check($signedUrl);
2655+
// $uriSignatureIsValid = true
2656+
2657+
// if you have access to the current Request object, you can use this
2658+
// other method to pass the entire Request object instead of the URI:
2659+
$uriSignatureIsValid = $this->uriSigner->checkRequest($request);
2660+
}
2661+
}
2662+
26202663
Troubleshooting
26212664
---------------
26222665

0 commit comments

Comments
 (0)