Installation
composer require 2lenet/media-bundle
Enable the Bundle
Add to config/bundles.php:
return [
// ...
Lle\MediaBundle\LleMediaBundle::class => ['all' => true],
];
Configure Routing
Add to config/routes.yaml:
media_routing:
resource: "@LleMediaBundle/Resources/config/routing.yaml"
Enable Doctrine Extensions
Ensure gedmo/doctrine-extensions is installed and configured in config/packages/doctrine.yaml:
gedmo_tree:
types:
tree: Lle\MediaBundle\Doctrine\Extensions\TreeType
First Use Case
Create a media entity (e.g., MediaItem) with a tree structure:
use Lle\MediaBundle\Entity\MediaInterface;
use Gedmo\Mapping\Annotation as Gedmo;
#[Gedmo\Tree(type: 'nested')]
class MediaItem implements MediaInterface
{
// ...
}
Hierarchical Media Management
Use the TreeType for nested media structures (e.g., folders/files):
$parent = $mediaRepository->findOneBy(['name' => 'Parent']);
$child = new MediaItem();
$child->setParent($parent);
$em->persist($child);
Route Generation Leverage the bundle’s routes for CRUD operations:
$url = $this->router->generate('lle_media_item_edit', ['id' => $media->getId()]);
Asset Handling
Extend MediaInterface to add custom fields (e.g., path, mimeType):
class CustomMediaItem extends MediaItem
{
#[ORM\Column(type: 'string')]
private string $customField;
}
Event Listeners
Hook into lifecycle events (e.g., prePersist) for validation:
$media->addEventListener(MediaEvents::PRE_PERSIST, function (MediaItem $media) {
if (empty($media->getName())) {
throw new \RuntimeException('Media name is required.');
}
});
Gedmo Tree Misconfiguration
gedmo/tree is installed and enabled in doctrine.yaml.php bin/console doctrine:schema:validate if tree queries fail.Route Conflicts
routing.yaml to avoid clashes with existing routes:
lle_media_item:
resource: "@LleMediaBundle/Resources/config/routing/item.yaml"
prefix: /media
Missing MediaInterface
MediaInterface; abstract classes won’t work.TreeRepository for debugging:
$children = $mediaRepository->findBy(['parent' => $media]);
APP_DEBUG=true) to inspect route generation.Custom Fields
Add fields to MediaInterface and update the bundle’s MediaType:
#[ORM\Column(type: 'json')]
private array $metadata;
Override Templates
Extend the bundle’s Twig templates by copying them to templates/LleMediaBundle/ and modifying.
API Integration
Use Symfony’s Serializer to expose media as JSON:
$serializer->serialize($media, 'json', ['groups' => ['media']]);
How can I help you explore Laravel packages today?