Installation:
composer require alphalemon/elfinder-bundle
(Note: The original README uses legacy Git submodule setup; Composer is preferred for modern Symfony projects.)
Enable the Bundle:
Add to config/bundles.php:
return [
// ...
AlphaLemon\ElFinderBundle\AlphaLemonElFinderBundle::class => ['all' => true],
];
Install Assets:
php bin/console assets:install public
php bin/console assetic:dump
First Use Case:
Access the file manager at /al_showElFinder (default route). This renders a ready-to-use elFinder UI with minimal configuration.
vendor/alphalemon/elfinder-bundle/Resources/views/ElFinder/show.html.twig
(Blocks for customization: stylesheet_files, javascript_files, init_script, elfinder_html)vendor/alphalemon/elfinder-bundle/Resources/config/services.yml
(Defines the default el_finder_media_connector service.)Extend the Default Template:
Override show.html.twig in your theme (e.g., templates/ElFinder/show.html.twig) to customize:
{% extends 'AlphaLemonElFinderBundle:ElFinder:show.html.twig' %}
{% block init_script %}
<script>
$(document).ready(function() {
$('<div/>').elfinder({
url: '/custom-connector',
lang: 'en',
width: '100%',
height: '500px'
}).dialog();
});
</script>
{% endblock %}
Configure the Connector:
Create a custom connector service in config/services.yaml:
services:
App\Connector\CustomElFinderConnector:
arguments: ['@service_container']
tags: ['el_finder.connector']
(Tagging ensures the service is autowired into the bundle.)
Define Connector Logic:
// src/Connector/CustomElFinderConnector.php
namespace App\Connector;
use AlphaLemon\ElFinderBundle\Core\Connector\AlphaLemonElFinderBaseConnector;
class CustomElFinderConnector extends AlphaLemonElFinderBaseConnector {
protected function configure() {
return [
'roots' => [
[
'driver' => 'LocalFileSystem',
'path' => '%kernel.project_dir%/public/uploads',
'URL' => '/uploads',
'accessControl' => 'access',
'uploadAllow' => ['all'], // Allow all uploads (customize as needed)
]
],
];
}
}
| Use Case | Implementation Pattern |
|---|---|
| File Uploads | Extend configure() to set 'uploadAllow' and 'uploadDeny'. |
| Custom Permissions | Override access() method in your connector to validate user roles. |
| Multiple Roots | Define an array of roots in configure() with different drivers (e.g., FTP, S3). |
| Integration with Forms | Use dataUrl in init_script to return file paths to a Symfony form field. |
| Thumbnails | Configure 'imageMimeTypes' and 'thumbnailSize' in roots options. |
Security: Use Symfony’s security component in your connector:
public function access($attr, $path = null) {
if ($attr === 'read') {
return $this->container->get('security.authorization_checker')->isGranted('ROLE_USER');
}
return false;
}
Routing:
Customize the connector route in config/routes.yaml:
al_custom_connector:
path: /custom-connector
defaults: { _controller: 'App\Controller\ElFinderController::connect' }
Assetic:
Add custom CSS/JS to the stylesheet_files or javascript_files blocks in your template.
Asset Installation:
assets:install or assetic:dump will break the UI.make:elfinder (if available in your project).Connector Caching:
configure() dynamically (e.g., based on user input), cache the service or use a factory.Path Configuration:
path must be an absolute filesystem path (e.g., /var/www/uploads).URL must be the publicly accessible URL (e.g., /uploads)../uploads) will fail.Driver Compatibility:
LocalFileSystem by default. For cloud storage (e.g., S3), install the elFinder S3 driver and configure it in roots.CSRF Protection:
# config/routes.yaml
al_custom_connector:
path: /custom-connector
defaults: { _controller: 'App\Controller\ElFinderController::connect' }
methods: [GET, POST]
requirements:
_method: GET|POST
Check Connector Output:
APP_DEBUG=true) and inspect the JSON response at /custom-connector for errors.Log Connector Configuration:
Add this to your connector’s configure() method:
error_log(print_r($options, true)); // Logs options to Symfony's error log.
Browser Console:
F12) and check the Network tab for failed AJAX requests to the connector.elFinder Console:
console.log(elFinder) in your init_script to inspect the elFinder instance.Custom Drivers:
Extend AlphaLemonElFinderBaseConnector to support non-standard storage (e.g., database files, API integrations).
Event Listeners: Subscribe to elFinder events (e.g., file uploads) using Symfony’s event dispatcher:
// src/EventListener/ElFinderListener.php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class ElFinderListener {
public function onKernelRequest(RequestEvent $event) {
if ($event->getRequest()->getPathInfo() === '/custom-connector') {
// Custom logic (e.g., log uploads)
}
}
}
Register in config/services.yaml:
services:
App\EventListener\ElFinderListener:
tags: ['kernel.event_listener', { event: 'kernel.request' }]
Twig Extensions: Create a Twig extension to dynamically generate elFinder configurations:
// src/Twig/ElFinderExtension.php
namespace App\Twig;
class ElFinderExtension extends \Twig_Extension {
public function getFunctions() {
return [
new \Twig_SimpleFunction('elfinder_config', [$this, 'getConfig']),
];
}
public function getConfig($options) {
return json_encode($options);
}
}
Use in Twig:
{% block init_script %}
<script>
$(document).ready(function() {
$('<div/>').elfinder({
url: '/custom-connector',
config: {{ elfinder_config({'lang': 'en', 'width': '100%'}) }}
});
});
</script>
{% endblock %}
Disable Thumbnails for Large Directories:
Set 'thumbnailSize' to false in roots if thumbnails are unnecessary.
Lazy-Load Roots:
Dynamically load roots based on user roles to reduce connector overhead:
protected function configure() {
$roots = [];
if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
$roots[] = ['driver' => 'LocalFileSystem', 'path' => '/admin-files', ...];
}
return ['roots' => $roots];
}
Cache Connector Responses: Use Symfony’s HTTP cache for the connector route if the file structure rarely changes:
# config/routes.yaml
al
How can I help you explore Laravel packages today?