Skip to content

Commit f85f301

Browse files
committed
Document UriSigner
1 parent 2174905 commit f85f301

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

routing.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,47 @@ 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 original hash.
2626+
2627+
Symfony provides a utility to sign URIs, no matter if you generated them yourself
2628+
with the methods explained above or if they were given to you. The utility is
2629+
implemented in the :class:`Symfony\\Component\\HttpKernel\\UriSigner` service,
2630+
which you can inject in your services or controllers::
2631+
2632+
// src/Service/SomeService.php
2633+
namespace App\Service;
2634+
2635+
use Symfony\Component\HttpKernel\UriSigner;
2636+
2637+
class SomeService
2638+
{
2639+
public function __construct(
2640+
private UriSigner $uriSigner,
2641+
) {
2642+
}
2643+
2644+
public function someMethod()
2645+
{
2646+
// ...
2647+
2648+
// generate a URL youself or get it somehow...
2649+
$url = 'https://example.com/foo/bar?sort=desc';
2650+
2651+
// sign the URL (it adds a query parameter called '_hash')
2652+
$signedUrl = $this->uriSigner->sign($url);
2653+
// $url = 'https://example.com/foo/bar?sort=desc&_hash=e4a21b9'
2654+
2655+
// check the URL signature
2656+
$uriSignatureIsValid = $this->uriSigner->check($signedUrl);
2657+
// $uriSignatureIsValid = true
2658+
}
2659+
}
2660+
26202661
Troubleshooting
26212662
---------------
26222663

0 commit comments

Comments
 (0)