Bundle for file management in symfony
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Open a command console, enter your project directory and execute:
composer require ics/medias-bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
composer require ics/medias-bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
// config/bundles.php
return [
// ...
ICS\MediaBundle\MediaBundle::class => ['all' => true],
];
For install database :
# Installer la base de données
php bin/console doctrine:schema:create
For update database :
# Mise a jour la base de données
php bin/console doctrine:schema:update -f
#config/routes.yaml
#...
media_bundle:
resource: '@MediaBundle/config/routes.yaml'
prefix: /medias
#...
You can configure relative path from the public directory. The medias files will be stored in this directory
# config/packages/medias.yaml
medias:
path: 'medias' # Default Value
The Mediabundle propose two FormType MediaType
that return One MediaFile and MediaCollectionType
that retun Many MediaFile. You may precise the outputdir relative to medias path in the config.
use ICS\MediaBundle\Entity\MediaImage;
// Exemple entity
public class User
{
/**
* Avatar of user
*
* @var MediaImage
* @ORM\ManyToOne(targetEntity=MediaImage::class, cascade={"persist","remove"})
*/
private $avatar;
/**
* Gallery of user
*
* @var ArrayCollection
* @ORM\ManyToMany(targetEntity=MediaImage::class, cascade={"persist","remove"})
*/
private $gallery;
public function __construct()
{
$this->gallery=new ArrayCollection();
}
}
//...
public function buildForm(FormBuilderInterface $builder, array $options): void
{
// For One file
$builder->add('avatar',MediaType::class,[
'outputdir' => 'user/avatar'
]);
// For Many files
$builder->add('gallery',MediaCollectionType::class,[
'outputdir' => 'user/gallery'
]);
}
//...
You can impose limitation of type for all and size for MediaCollectionType
//...
public function buildForm(FormBuilderInterface $builder, array $options): void
{
// For One file
$builder->add('avatar',MediaType::class,[
'outputdir' => 'user/avatar',
'required' => false,
'acceptedFiles' => ['image/jpeg','.jpg'],
]);
// For Many files
$builder->add('gallery',MediaCollectionType::class,[
'outputdir' => 'user/gallery'
'required' => false,
'acceptedFiles' => MediaImage::$mimes
'maxFileSize' => '1024' // 1Ko
]);
}
//...
The maxFileSize is calculated based on system capabilities and your configuration. If your configuration is higher of system capabilities, system capabilities is used.
Two commands are implemented
php bin/console media:file:verify
php bin/console media:search:duplicate
Adding bundle to EasyAdmin
Add this MenuItems in your dashboard Controller/Admin/DashboardController.php
// Controller/Admin/DashboardController.php
use ICS\SsiBundle\Entity\User;
use ICS\SsiBundle\Entity\Log;
class DashboardController extends AbstractDashboardController
{
public function configureMenuItems(): iterable
{
// ...
yield MenuItem::section('Medias', 'fa fa-photo-video');
yield MenuItem::linkToCrud('Files', 'fa fa-file', MediaFile::class);
yield MenuItem::linkToCrud('Pictures', 'fa fa-photo', MediaImage::class);
// ...
}
}
{% extends "@EasyAdmin/page/content.html.twig" %}
{% block page_content %}
{{ mediaGraphData() }}
{% endblock %}