Overview
TheAttachmentController handles secure delivery of private attachment files, ensuring only authorized users can access their own files.
Namespace: App\Http\Controllers
Class: AttachmentController
Methods
show()
Retrieves and serves a private attachment file to the authenticated user. Route:GET /private-attachment/{attachment}
Route Name: private_attachment.show
Middleware: auth, verified
Authentication: Required
Description
This method serves attachment files stored in local storage. It performs authorization checks to ensure:- The requesting user owns the attachment
- The file exists in storage
Request Parameters
| Parameter | Type | Location | Required | Description |
|---|---|---|---|---|
| attachment | Attachment | Route | Yes | Attachment model instance (route model binding) |
Response
Success (200): File content with appropriate MIME type Forbidden (403): User does not own the attachment- Message: “No tienes permiso para ver este archivo.”
- Message: “El archivo no se encuentra.”
Code Example
HTTP Request Example
Usage in Blade
Error Responses
Security
- Route model binding automatically validates attachment ID
- Ownership verification prevents unauthorized access
- File existence check prevents path traversal attempts
- Uses Laravel’s local storage disk for secure file handling
Dependencies
App\Models\AttachmentIlluminate\Support\Facades\Storage
