Core\Router::signedRoute
Core\Router::signedRoute(array<secret bytes> $keys): Core\Router\MatchCore\Router::signedRoute( array<secret bytes> $keys, ): Core\Router\Match
Checks that a request arrived with a signed link that your program made. Core\Router::urlSigned makes the link, and Core\Router::signedRoute checks it when the request arrives. You give it the same keys, newest first. It returns the route that the request matched, with its values.
The check covers the route's path, such as /unsubscribe/{reader}, every value and the time when the link stops working. If somebody changes a value, removes the _sig parameter or adds a second one, signedRoute throws a RuntimeError. A link that is too old also throws a RuntimeError, with its own message.
The method's name is not part of the check. If you rename the method, old links still work. If you change the path in its #[Route], old links stop working.
Nothing checks a signature for you. Call signedRoute at the start of each handler that needs a signed link, and decide there what the visitor sees when the check fails.
The examples show a valid download link, a link with a changed value, and an unsubscribe link that was signed with an old key.
Parameters
$keysarray<secret bytes>The keys, newest first. Give the same keys that you giveurlSigned. A signature made with any key in the list is accepted. So you can add a new key and keep the old one until the old links stop working.
Return value
Returns Core\Router\Match. The route that the request matched, as a Core\Router\Match. It is the same value that Core\Request::route() returns. When the check fails, this method throws an error and returns nothing.
Errors
RuntimeErrorWhen the signature is not valid: there is no_sigparameter or there are two, the signature was changed, a key that is not in$keysmade it, a value was added, removed or changed, or the request matched no route with a name. All of these have the same message. When the link is too old, with a message of its own. When an escape in the query gives bytes that are not UTF-8.LogicErrorWhen the program is not handling a request, for example a command-line program, a job or a test. When$keysis empty. When its first key is not 32 bytes long.
Examples
Checking a download link
<?nvs// Shows how to check the signed link that a request arrived with.
class Reports{ #[Core\Route(path: '/reports/{year}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function download(uint $year): string { return 'the report'; }}
// A real program reads its key from its configuration. This key is written here,// so that the signed link in the request file matches it. A key is 32 bytes.bytes $raw = 'example-key-for-signed-links-001' as bytes;array<secret bytes> $keys = [$raw as secret bytes];
// The request is `/reports/2024?_sig=...`, made by `Core\Router::urlSigned`.// `signedRoute` checks the signature and returns the route that matched.Core\Router\Match $matched = Core\Router::signedRoute($keys);
// `is` checks which method the route belongs to.// This prints `Reports::download` and then `year 2024`.echo $matched->is(Reports::download) ? 'Reports::download' : 'another route', "\n";echo 'year ', $matched->parameter('year') as uint, "\n";Reports::downloadyear 2024A link with a changed value
<?nvs// Shows what happens when somebody changes a value in a signed link.
class Reports{ #[Core\Route(path: '/reports/{year}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function download(uint $year): string { return 'the report'; }}
bytes $raw = 'example-key-for-signed-links-001' as bytes;array<secret bytes> $keys = [$raw as secret bytes];
// The link was signed for 2024, but the visitor changed the path to `/reports/2025`.// So `signedRoute` throws a `RuntimeError`, and this prints `The link is not valid.`try { Core\Router::signedRoute($keys); echo "The report for 2025.\n";} catch (RuntimeError $e) { echo "The link is not valid.\n";}The link is not valid.An unsubscribe link after a key change
<?nvs// Shows how a newsletter accepts an unsubscribe link that was signed with its old key.
class Newsletter{ #[Core\Route(path: '/unsubscribe/{reader}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function unsubscribe(uint $reader): string { return 'you are unsubscribed'; }}
// The newsletter changed its key last month. The new key comes first.// The old key stays in the list, so the links in old emails still work.bytes $current = 'example-key-for-signed-links-002' as bytes;bytes $older = 'example-key-for-signed-links-000' as bytes;array<secret bytes> $keys = [$current as secret bytes, $older as secret bytes];
// The link in the request was signed with the old key.// This prints `Reader 17 is unsubscribed.`try { Core\Router\Match $matched = Core\Router::signedRoute($keys); uint $reader = $matched->parameter('reader') as uint; echo 'Reader ', $reader, " is unsubscribed.\n";} catch (RuntimeError $e) { echo "This link is not valid. Please use the link in your latest email.\n";}Reader 17 is unsubscribed.