Install the Bundle
composer require backend2-plus/upload-bundle
Configure Flysystem
Add filesystem adapters and filesystems in config/packages/oneup_flysystem.yaml:
oneup_flysystem:
adapters:
public_uploads:
local:
directory: '%kernel.project_dir%/public/upload'
filesystems:
public_uploads_filesystem:
adapter: public_uploads
First Upload Use the provided API endpoint:
POST /api/upload
With a multipart/form-data request containing a file.
Service Injection
Inject UploadService in your controller/service:
use Backend2Plus\UploadBundle\Service\UploadService;
public function __construct(private UploadService $uploadService) {}
Single File Upload
$file = $request->file('file');
$uploadedFile = $this->uploadService->upload($file, 'public_uploads_filesystem');
return $uploadedFile->getUrl(); // Returns the accessible URL
Multiple File Upload
Use MultipleUploadBundleService:
$files = $request->files->all();
$uploadedFiles = $this->multipleUploadService->uploadMultiple($files, 'public_uploads_filesystem');
return array_map(fn($file) => $file->getUrl(), $uploadedFiles);
File Validation
Extend validation via UploadService:
$this->uploadService->upload($file, 'public_uploads_filesystem', [
'allowed_mime_types' => ['image/jpeg', 'image/png'],
'max_size' => 5 * 1024 * 1024, // 5MB
]);
MediaObject Entity
The bundle assumes an existing MediaObject entity. Ensure it’s mapped to the filesystem:
// Example MediaObject mapping
/**
* @ORM\Column(type="string")
*/
private $path;
/**
* @ORM\Column(type="string")
*/
private $url;
Post-Upload Logic
Attach metadata to MediaObject after upload:
$mediaObject = new MediaObject();
$mediaObject->setPath($uploadedFile->getPath());
$mediaObject->setUrl($uploadedFile->getUrl());
$mediaObject->setMimeType($uploadedFile->getMimeType());
$entityManager->persist($mediaObject);
Override the default /api/upload endpoint by:
UploadController.config/routes.yaml:
upload:
path: /custom/upload
controller: App\Controller\CustomUploadController::upload
Filesystem Permissions
Ensure directories (public/upload, privatePictures) are writable by the web server:
chmod -R 775 %kernel.project_dir%/public/upload
MediaObject Entity Mismatch
The bundle expects a MediaObject entity with path, url, and mimeType fields. Verify your entity matches:
// If missing, add:
/**
* @ORM\Column(type="string", nullable=true)
*/
private $mimeType;
Flysystem Adapter Conflicts
Avoid naming conflicts with existing adapters/filesystems in oneup_flysystem.yaml.
Deprecated Service Namespace
Old code using App\Bundles\Upload\MultipleUploadBundleService must update to the new namespace:
// Old (deprecated)
use App\Bundles\Upload\MultipleUploadBundleService;
// New
use Backend2Plus\UploadBundle\Service\App\Bundles\Upload\MultipleUploadBundleService;
Upload Failures
Check Symfony’s ProblemDetails response for errors (e.g., invalid MIME type, size limit).
Enable debug mode to log filesystem issues:
# config/packages/dev/oneup_flysystem.yaml
oneup_flysystem:
debug: true
File Path Resolution
Use UploadService::getFilesystem() to inspect the active filesystem:
$filesystem = $this->uploadService->getFilesystem('public_uploads_filesystem');
$filesystem->listContents('/'); // Debug contents
Custom Storage Adapters
Extend Flysystem adapters (e.g., S3) by adding to oneup_flysystem.yaml:
adapters:
s3_uploads:
aws:
bucket: 'my-bucket'
region: 'us-east-1'
Pre/Post-Upload Hooks
Subscribe to events (if the bundle supports them) or wrap UploadService:
$uploadedFile = $this->uploadService->upload($file, 'fs');
$this->postUploadLogic($uploadedFile); // Custom logic
Validation Rules Dynamically set rules via dependency injection:
# config/services.yaml
Backend2Plus\UploadBundle\Service\UploadService:
arguments:
$defaultValidationRules:
allowed_mime_types: ['image/*']
max_size: 10MB
How can I help you explore Laravel packages today?