Laravel Signed URLs
Lesson 27 of 33 in the Laravel learning path.
How do I share a temporary lesson link that fails if someone edits the query?
URL::temporarySignedRoute adds a signature and an expiry. The signed middleware checks both.
A preview link for an unpublished lesson should not work after you change the id in the query string, and it should die at a known time.
Syntax & Parameters
URL::temporarySignedRoute('lessons.preview', now()->addHour(), $lesson);
Example
<?php
use App\Http\Controllers\LessonController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
Route::get('/lessons/{lesson}/preview', [LessonController::class, 'preview'])
->name('lessons.preview')
->middleware('signed');
function previewLink(int $lessonId): string
{
return URL::temporarySignedRoute(
'lessons.preview',
now()->addHour(),
['lesson' => $lessonId]
);
}
Expected Output
A URL with signature and expires query keys. A valid hit within the hour reaches the preview. A tampered id or a late visit is rejected.
Line-by-Line Walkthrough
- The preview route is named lessons.preview.
- The signed middleware rejects a missing or edited signature.
- temporarySignedRoute sets a one-hour expiry.
- The lesson id is a route parameter, not a free-form query you forget to sign.
- Changing the id invalidates the signature.
- After an hour the link returns 403.
- Generate the link on the server after the policy allows it.
- Do not build the signature yourself.
Common Pitfalls & Security
Check the edge before you ship it.
- A signed URL is a capability. Anyone who has it can open it until it expires.
- Logging the full URL stores that capability.
- Forgetting the signed middleware makes the signature decorative.
- Clock skew can expire a link early. Keep the window reasonable.
Best Practices & Modern Standards
Use temporary signed routes for one-hour previews and email confirmations.
Put the middleware on the route. Keep the TTL short.
Real-World Use
An instructor emails a one-hour preview of an unpublished lesson. Editing the id in the link fails the signature.
Frequently Asked Questions
What does the signed middleware check?
The signature matches the URL and, for a temporary link, the expiry has not passed.
What status is a bad signature?
403.
Can I sign a POST?
Yes, but email links are GET. Do not put a state change on a signed GET.
Is this a login?
No. It proves the URL was minted by the app, not who is holding it.