Installation
composer require antwebes/foto-bundle
Ensure your composer.json includes "symfony/framework-bundle": ">=2.1" (or equivalent for Symfony 2.x).
Enable the Bundle
Add to app/AppKernel.php:
new Antwebes\FotoBundle\AntwebesFotoBundle(),
Configure VichUploader
Update config.yml:
vich_uploader:
db_driver: orm
storage: file_system
mappings:
foto:
uri_prefix: /uploads/images
upload_destination: '%kernel.project_dir%/web/uploads/images'
namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
First Use Case: Uploading an Image
Create an entity (e.g., Product) with a FotoType field:
use Antwebes\FotoBundle\Entity\Foto;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Product {
/**
* @ORM\OneToOne(targetEntity="Foto", mappedBy="product")
*/
private $foto;
}
Use the FotoType form in your controller:
use Antwebes\FotoBundle\Form\Type\FotoType;
$form = $this->createForm(FotoType::class, $product);
Entity Setup
Extend Foto entity or embed it in your model:
class Article {
/**
* @ORM\OneToOne(targetEntity="Antwebes\FotoBundle\Entity\Foto", cascade={"persist"})
* @Assert\Valid
*/
private $coverImage;
}
Form Integration
Use FotoType in your form builder:
$builder->add('coverImage', FotoType::class, [
'label' => 'Cover Image',
'required' => false,
'allow_file_replace' => true, // Enable file replacement
]);
Handling Uploads In your controller, handle the form submission:
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($product);
$em->flush();
// FotoBundle auto-handles uploads via VichUploader
}
Displaying Images
Use the foto_image Twig filter:
<img src="{{ product.coverImage.pathAndUrl }}"/>
Or access the path directly:
$imagePath = $product->getCoverImage()->getPath();
namer in config.yml for unique filenames:
mappings:
foto:
namer: App\Namer\CustomNamer
FotoType:
$builder->add('file', FileType::class, [
'constraints' => [
new File\FileType('image/jpeg'),
new File\MaxSize('2M'),
],
]);
FotoManager for bulk operations:
$manager = $this->get('antwebes_foto.manager');
$manager->deleteAllImagesForEntity($product);
Symfony 2.x Compatibility
vich/uploader-bundle) if migrating.File Permissions
web/uploads/images is writable:
chmod -R 775 %kernel.project_dir%/web/uploads/images
vich_uploader.log for permission errors.Missing Documentation
index.rst lacks details on advanced features (e.g., custom storage).Entity Lifecycle
preRemove in your entity:
public function preRemove() {
$this->getCoverImage()->remove();
}
var/log/dev.log for VichUploader exceptions.uri_prefix in config.yml matches your web root.services.yml:
services:
antwebes_foto.storage:
class: App\Storage\S3Storage
arguments: ['@aws_sdk.s3']
vich_uploader.upload events for post-upload logic:
services:
app.foto_listener:
class: App\EventListener\FotoListener
tags:
- { name: kernel.event_listener, event: vich_uploader.upload, method: onUpload }
foto_image filter for custom processing:
class FotoExtension extends \Twig_Extension {
public function getFilters() {
return [
new \Twig_SimpleFilter('resize_foto', [$this, 'resizeImage']),
];
}
}
How can I help you explore Laravel packages today?