- How do I integrate tus-php into a Laravel project for resumable file uploads?
- Start by requiring the package via Composer: `composer require ankitpokhrel/tus-php`. Then, configure a route in `routes/web.php` using the `TusController` (e.g., `Route::tus('upload', TusController::class)`). Ensure your storage backend (e.g., S3, local filesystem) is set up in Laravel’s `config/filesystems.php`. The package leverages Laravel’s filesystem contracts, so no extra setup is needed for most backends.
- Does tus-php support Laravel’s built-in storage adapters like S3 or FTP?
- Yes, tus-php works seamlessly with Laravel’s filesystem adapters. Configure your preferred disk (e.g., `s3`, `local`) in `config/filesystems.php`, then reference it in the tus configuration. For example, set `'storage' => 's3'` in tus-php’s config to use AWS S3. The package inherits Laravel’s storage abstraction, so switching backends is straightforward.
- Can I use tus-php with Laravel 8+ and PHP 8.0+ only, or does it support older versions?
- The package officially supports PHP 8.0+ and Laravel 8+. While it may work with older versions, compatibility isn’t guaranteed. Pin the package version in `composer.json` (e.g., `^1.0`) to avoid breaking changes. For Laravel 7 or PHP 7.x, check the package’s release notes or fork it for custom adjustments.
- How do I handle authentication for tus uploads in Laravel? Tus lacks built-in auth—what’s the best approach?
- Use Laravel middleware to validate authentication before tus requests. For API tokens, add `auth:api` to your tus route (e.g., `Route::tus('upload', TusController::class)->middleware('auth:api')`). For session-based auth, use `web` middleware. Alternatively, create a custom middleware to validate headers or cookies before tus processing. Tus-php itself doesn’t enforce auth; it’s your responsibility to gate access.
- Are there Laravel events or hooks to trigger actions after a tus upload completes (e.g., processing files in a queue)?
- Yes, tus-php emits events like `UploadStarted`, `UploadCompleted`, and `UploadFailed` that you can bind to Laravel’s event system. In your `EventServiceProvider`, listen for these events and dispatch Laravel jobs (e.g., `FileUploaded::dispatch($file)`). For example, use `UploadCompleted` to queue a video encoding job or send a notification. Check the package’s event documentation for exact event names.
- How does tus-php handle temporary files during resumable uploads? Will partial uploads clutter my storage?
- Tus-php manages temporary files automatically using the configured storage backend. Partial uploads are stored as `.tmp` files (e.g., `filename.tmp`) until the upload completes. Once finished, the file is moved to its final location. No clutter occurs if the upload fails—temporary files are cleaned up. For custom storage, ensure your adapter handles `.tmp` files correctly.
- Can I use tus-php behind a load balancer with multiple servers? What about concurrency issues?
- Tus-php is stateless for individual requests but relies on storage backends to track upload state. For distributed setups, ensure all servers use the same storage (e.g., shared S3 bucket or database-backed storage). Concurrency risks arise if multiple servers process the same upload simultaneously. Mitigate this by implementing upload locking (e.g., a `locked_at` field in a database table) or using a centralized queue for upload processing.
- How do I configure CORS and CSRF protection for tus uploads in Laravel?
- Tus uploads are typically initiated via JavaScript clients (e.g., tus-js), so CORS must be enabled. Add CORS headers in your tus route middleware or globally via `app/Http/Middleware/Cors.php`. For CSRF protection, exclude tus routes from Laravel’s `VerifyCsrfToken` middleware (add `'upload/*'` to the `$except` array). Tus-php doesn’t handle CSRF; Laravel’s middleware must validate it separately.
- Are there performance considerations for large files (e.g., video uploads) with tus-php?
- Tus-php is optimized for chunked uploads, but performance depends on chunk size, network stability, and storage backend. For large files, adjust the `chunk_size` in tus-php’s config (default: 5MB) to balance memory usage and transfer efficiency. Monitor disk I/O if using local storage, and consider async processing (e.g., Laravel queues) for post-upload tasks like thumbnail generation.
- What are the alternatives to tus-php for Laravel resumable uploads, and when should I choose them?
- Alternatives include `spatie/laravel-resumable-upload` (simpler but less protocol-compliant) or self-hosted solutions like `tusd` (the reference tus server). Choose tus-php if you need tus protocol compliance, Laravel integration, and flexibility with storage backends. Use spatie’s package for lightweight needs or `tusd` for non-PHP environments. Evaluate based on your need for client compatibility (tus-php supports all tus clients) and Laravel-specific features.