Installation
composer require docdigital/cdn-bundle:dev-master
(Note: Use dev-master as the package lacks stable releases.)
Enable the Bundle
Add to AppKernel.php:
new DocDigital\Bundle\CdnBundle\DdCdnBundle(),
Configure Upload Directory
In app/config/parameters.yml:
parameters:
dd_cdn.upload.dir: "%kernel.root_dir%/../media/"
Ensure the directory exists and has write permissions for your web server user (e.g., www-data or apache).
First Use Case: File Upload
Inject the CdnManager service in a controller/service:
use DocDigital\Bundle\CdnBundle\Manager\CdnManager;
class UploadController extends Controller
{
public function uploadAction(Request $request, CdnManager $cdnManager)
{
$file = $request->files->get('file');
$path = $cdnManager->upload($file, 'user_uploads'); // 'user_uploads' = subdirectory
return new Response("File saved at: $path");
}
}
File Uploads
CdnManager::upload() to store files in media/{subdir}/.$cdnManager->upload($file, 'avatars', 'unique_filename.png');
(Omit filename to auto-generate.)File Downloads
CdnManager::getUrl() to generate public URLs:
$url = $cdnManager->getUrl('avatars/unique_filename.png');
web/media) for direct access:
$cdnManager->symlink('avatars/unique_filename.png', 'public_media');
Subdirectories
users/, products/):
$cdnManager->upload($file, 'products/123');
Deletion
$cdnManager->delete('avatars/old_file.png');
Symlinks for Public Access
Configure a cron job or event listener to periodically symlink files to web/media/ for direct HTTP access:
$cdnManager->symlinkAll('media/', 'web/media');
Validation Validate file types/sizes before upload:
if ($file->getClientOriginalExtension() !== 'png') {
throw new \InvalidArgumentException('Only PNG allowed.');
}
Event Listeners Trigger actions post-upload (e.g., image resizing):
$cdnManager->upload($file, 'thumbnails')->then(function($path) {
// Resize logic here
});
Permissions
media/ lacks write permissions for the web server user.chown -R www-data:www-data media/ (Linux) or adjust parameters.yml to a writable directory.Symlink Conflicts
web/media may cause conflicts if files are manually added to web/.web/cdn_media) or validate symlinks before creation.No CDN Integration
CdnManager or use a separate service for CDN uploads.Filename Collisions
uniqid() or UUIDs:
$cdnManager->upload($file, 'uploads', uniqid().'.'.$file->getClientOriginalExtension());
No File Overwrite Protection
upload() overwrites files by default.File::exists() checks.Log Upload Paths Enable debug mode to log paths:
$this->get('logger')->debug('Uploaded to:', [$path]);
Check File Existence Verify files exist post-upload:
if (!file_exists($cdnManager->getPath('avatars/test.png'))) {
throw new \RuntimeException('File not saved.');
}
Custom Storage
Override CdnManager to support S3 or other storage:
class CustomCdnManager extends CdnManager {
public function upload($file, $subdir = null, $filename = null) {
// Custom logic (e.g., S3 upload)
}
}
Events Dispatch events for uploads/deletions:
$event = new FileUploadEvent($file, $path);
$this->get('event_dispatcher')->dispatch('cdn.upload', $event);
Configuration Dynamically set upload dirs via dependency injection:
services:
cdn.manager:
arguments:
- "%dd_cdn.upload.dir%"
How can I help you explore Laravel packages today?