codeconsortium/ccdn-component-attachment-bundle
Installation
composer require codeconsortium/ccdn-component-attachment-bundle:dev-master
(Note: Bundle is outdated; ensure Symfony 2.1.x/PHP 5.4 compatibility in your project.)
Enable the Bundle
Add to app/AppKernel.php:
new CodeConsortium\CCDNComponentAttachmentBundle\CCDNComponentAttachmentBundle(),
Configure Database (Optional)
The bundle uses Doctrine 2.1.x but does not require a specific DB schema. Ensure your config.yml includes:
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
First Use Case: Uploading Attachments
Attachment entity or use the provided controller (AttachmentController).routing.yml:
ccdn_attachment:
resource: "@CCDNComponentAttachmentBundle/Resources/config/routing.yml"
prefix: /attachments
/attachments/new.Uploading Files
AttachmentType form (Symfony 2.1 form builder) to handle uploads:
$form = $this->createForm(new AttachmentType());
uploads/ (default) or a configurable directory. Thumbnails are auto-generated for images.Attaching to Entities
Post, Comment) via a many-to-many relationship:
/**
* @ORM\ManyToMany(targetEntity="CodeConsortium\CCDNComponentAttachmentBundle\Entity\Attachment")
*/
private $attachments;
AttachmentListener to auto-populate attachments during entity persistence.Displaying Attachments
{% for attachment in post.attachments %}
<img src="{{ asset(attachment.getThumbnailPath()) }}" alt="{{ attachment.name }}">
<a href="{{ path('ccdn_attachment_download', {'id': attachment.id}) }}">Download</a>
{% endfor %}
AttachmentHelper for utility methods (e.g., getFileExtension()).Deleting Files
delete() method on the Attachment entity or use the controller’s deleteAction:
$attachment->delete(); // Handles file + DB cleanup
AttachmentStorage service to use S3/AWS:
# app/config/config.yml
ccdn_attachment.storage:
class: Your\Custom\Storage\Service
arguments: ["@your_s3_client"]
AttachmentType to add constraints:
$builder->add('file', 'file', [
'max_size' => '10M',
'mime_types' => ['image/jpeg', 'application/pdf'],
]);
attachment.pre_upload or attachment.post_delete to hook into workflows:
$dispatcher->addListener('attachment.pre_upload', function($event) {
// Log or transform files before upload
});
Symfony 2.1 Legacy Code
FormType, Templating). Update or wrap calls if using newer Symfony.FormType:
// Replace:
$form = $this->createForm(new AttachmentType());
// With:
$form = $this->createForm(AttachmentType::class);
Thumbnail Generation
Imagick/GD is missing. Add a fallback:
if (!$attachment->hasThumbnail()) {
$attachment->setThumbnailPath('/path/to/fallback.png');
}
config.yml:
ccdn_attachment:
allowed_thumbnail_types: [jpeg, png, gif]
File Permissions
uploads/ directory is writable:
chmod -R 775 app/uploads
umask in your upload script.Database Schema
Attachment entity. If extending, regenerate migrations:
php app/console doctrine:schema:update --force
doctrine:
dbal:
logging: true
AttachmentStorage:
$this->get('ccdn_attachment.storage')->getUploadPath(); // Should return absolute path
php app/console cache:clear
Custom File Processors
CodeConsortium\CCDNComponentAttachmentBundle\Processor\FileProcessorInterface to add watermarks, virus scanning, etc.:
class WatermarkProcessor implements FileProcessorInterface {
public function process(File $file) {
// Add watermark logic
return $file;
}
}
services.yml:
ccdn_attachment.processor.watermark:
class: Your\WatermarkProcessor
tags:
- { name: ccdn_attachment.processor }
Override Templates
Resources/views/ from the bundle to app/Resources/CCDNComponentAttachmentBundle/ to customize Twig templates.API Endpoints
AttachmentController to add JSON responses:
public function apiUploadAction(Request $request) {
$form = $this->createForm(AttachmentType::class);
$form->handleRequest($request);
if ($form->isSubmitted()) {
return new JsonResponse(['success' => true, 'id' => $attachment->getId()]);
}
return new JsonResponse(['success' => false], 400);
}
How can I help you explore Laravel packages today?