c33s/attachment-admin-bundle
Install the Bundle Require the package via Composer:
composer require c33s/attachment-admin-bundle
Register the bundle in config/bundles.php:
return [
// ...
C33s\AttachmentAdminBundle\C33sAttachmentAdminBundle::class => ['all' => true],
];
Enable Propel ORM (if not already)
The bundle assumes Propel is used. Ensure propel/propel-bundle is installed and configured.
Generate a Model with Attachments
Use the c33s:attachment:generate command to scaffold attachment fields for an existing model:
php bin/console c33s:attachment:generate App\Entity\YourModel
This creates:
Attachment table (if missing).YourModelAttachment join table (if missing).YourModelAdmin class extending SonataAdminBundle with attachment fields.First Use Case: Upload Files
/admin/your_model).Extend an Existing Sonata Admin Override the default admin class to customize attachment behavior:
use C33s\AttachmentAdminBundle\Admin\AttachmentAdmin;
class YourModelAdmin extends AttachmentAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('attachments', 'c33s_attachment', [
'label' => 'Attachments',
'required' => false,
'multiple' => true,
]);
}
}
Dynamic Attachment Fields
Use the c33s_attachment form type to dynamically attach files to any model:
# config/sonata_admin.yml
sonata_admin:
templates:
form:
- 'C33sAttachmentAdminBundle:Form:field_c33s_attachment.html.twig'
Validation and Constraints Add validation rules to the attachment field:
$formMapper->add('attachments', 'c33s_attachment', [
'constraints' => [
new File\FileType(['mime_types' => ['image/jpeg', 'application/pdf']]),
new File\MaxSize(['maxSize' => '1024k']),
],
]);
Batch Processing
Use the AttachmentManager service to handle bulk operations:
$attachmentManager = $this->get('c33s_attachment.manager');
$attachments = $attachmentManager->findBy(['objectId' => $model->getId()]);
Custom Storage Backends Override the default storage (e.g., switch from filesystem to S3):
# config/packages/c33s_attachment_admin.yaml
c33s_attachment_admin:
storage:
adapter: 'aws' # or 'local', 'ftp', etc.
options:
bucket: 'your-bucket'
region: 'us-east-1'
Propel Dependency
schema.xml includes the Attachment and join tables. Run:
php bin/console propel:schema:update --force
Admin Route Conflicts
SonataAdminBundle, ensure the admin class name follows conventions (e.g., YourModelAdmin).class YourModelAdmin extends AttachmentAdmin
{
public function getRouteName()
{
return 'custom_your_model_admin';
}
}
File Upload Limits
upload_max_filesize and post_max_size may block large uploads. Adjust in php.ini or .htaccess:
upload_max_filesize = 20M
post_max_size = 25M
Permission Issues
www-data) has write permissions to the upload directory (default: var/uploads/).Translation Missing
php bin/console translation:update --force fr
Check Database Schema
Verify the Attachment and join tables exist:
SELECT * FROM attachment;
SELECT * FROM your_model_attachment;
Enable Debug Mode
Temporarily set APP_DEBUG=1 in .env to see detailed errors.
Log Uploads Add logging to track uploads:
$this->get('logger')->info('Attachment uploaded', ['file' => $file->getClientOriginalName()]);
Clear Cache After generating admins or updating config:
php bin/console cache:clear
Customize the Upload Form Override the Twig template:
# templates/YourModelAdmin/attachment_field.html.twig
{% extends 'C33sAttachmentAdminBundle:Form:field_c33s_attachment.html.twig' %}
{% block attachment_widget %}
{{ parent() }} <!-- Customize here -->
{% endblock %}
Add Metadata to Attachments
Extend the Attachment model to store extra fields (e.g., title, description):
<!-- schema.xml -->
<table name="attachment" phpName="Attachment">
<column name="title" type="VARCHAR" size="255" required="false" />
</table>
Hook into Lifecycle Events Use Propel listeners to trigger actions on attachment changes:
$attachment->save();
$this->get('event_dispatcher')->dispatch(
'attachment.pre_save',
new AttachmentEvent($attachment)
);
API Integration Expose attachments via API using FOSRestBundle or API Platform:
# config/serializer/Attachment.yaml
App\Entity\Attachment:
attributes:
url:
accessor:
getter: getUrl
serializer_name: 'serializer.group.default'
How can I help you explore Laravel packages today?