Add signed_route() and temporary_signed_route() global helpers (Laravel 12.x)
#58647
Replies: 3 comments 1 reply
-
|
Wouldn't it make more sense to change the if (! function_exists('route')) {
/**
* Generate the URL to a named route.
*
* @param \BackedEnum|string|null $name
* @param mixed $parameters
* @param bool $absolute
*/
function route($name = null, $parameters = [], $absolute = true): string
{
if ($name === null) {
return new class {
public function __construct(
private UrlGenerator $urlGenerator,
) {}
/**
* Create a signed route URL for a named route.
*
* @param string $name
* @param mixed $parameters
* @param \DateTimeInterface|\DateInterval|int|null $expiration
* @param bool $absolute
* @return string
*
* @throws \InvalidArgumentException
*/
public function signed($name, $parameters = [], $expiration = null, $absolute = true)
{
return $this->urlGenerator->signedRoute($name, $Parameters, $expiration, $absolute);
}
}
}
return app('url')->route($name, $parameters, $absolute);
}
}Of course, the anonymous class is for presentation purposes only. |
Beta Was this translation helpful? Give feedback.
-
|
Btw, you also have |
Beta Was this translation helpful? Give feedback.
-
|
I've actually written some of these helpers before for my own convenience, so I really do get where you are coming from. For actually having them in the laravel framework, where would you draw the line? route() -> signed_route() -> temporary_signed_route() The list probably goes on. As much as I'd like to say yes, I'd probably have to say we should stick with the URL facade when it comes to this :) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! I’d like to propose adding two small global helpers to Laravel’s helper set to complement the existing
route()helper when working with signed URLs.Motivation
Laravel provides:
route()— ergonomic global helper for standard named route URLsURL::signedRoute()/URL::temporarySignedRoute()— for signed URLsurl()->signedRoute() / url()->temporarySignedRoute()— same APIs via the URL generatorIn practice, when generating links in Blade/email templates, “plain” routes often use
route(), while signed routes require switching toURL::...orurl()->....This is a small but frequent friction point.A global helper would:
route(),url(), etc.)Proposal
Add two additive, backward-compatible global helpers:
signed_route()— creates a signed URL for a named routetemporary_signed_route()— creates a signed URL with expirationBoth are thin wrappers around existing framework functionality.
Suggested signatures (Laravel 12.x)
Where:
$nameis the named route$parametersare the usual route parameters / query parameters$absolutematches the existing signed URL methods (allow relative signed URLs)$expirationmatchesURL::temporarySignedRoute()accepted typesUsage examples
Basic signed link
Temporary signed link (30 minutes)
Relative signed link (useful behind proxies / varying hostnames)
Blade / email templates
<a href="{{ temporary_signed_route('verify.email', now()->addMinutes(60), ['id' => $user->id]) }}"> Verify Email </a>Implementation notes
URL::signedRoute()andURL::temporarySignedRoute()internally.function_exists) to remain consistent with existing helpers.route()), to keep related helpers grouped.Tests
I’m happy to include tests covering:
signed_route()includes a valid signaturetemporary_signed_route()includes expires and a valid signatureabsolute: falseproduces a relative URL and validates correctly (relative signature validation path)Docs
I can also update the URL generation / Signed URLs documentation to mention these helpers as convenience alternatives to the
URL::facade andurl()generator.Questions for maintainers
Are the helper names (
signed_route,temporary_signed_route) acceptable, or would you prefer a different naming scheme for consistency?Any preference on whether $absolute should be positional-only (like many helpers) vs named argument support?
If there’s interest, I’m ready to open a PR against 12.x with code + tests + docs.
Beta Was this translation helpful? Give feedback.
All reactions