laravel-elfinder

repository·master·Indexed 20 days ago

https://github.com/barryvdh/laravel-elfinder

An elFinder integration for Laravel that provides file management capabilities. It supports Laravel's filesystem disks (local or cloud), Glide for image processing, and integrations with editors such as TinyMCE (3.x, 4.x, 5.x) and CKEditor 4.x. The package includes a custom Artisan command `elfinder:publish` for asset management and allows for custom access control via `Elfinder::checkAccess` and custom backend implementations by extending the `Connector` class.

Tokens
4K
Snippets
15
Records
19
Agent score
72%

What's inside laravel-elfinder

  1. Integrate elFinder with TinyMCE 3.x

    master

    Use the elfinder.tinymce route. In your TinyMCE initialization, set file_browser_callback to the string 'elFinderBrowser' and implement the function using tinyMCE.activeEditor.windowManager.open.

    // TinyMCE init
    file_browser_callback : 'elFinderBrowser'
    
    // Implementation
    function elFinderBrowser (field_name, url, type, win) {
      var elfinder_url = '/elfinder/tinymce';
      tinyMCE.activeEditor.windowManager.open({
        file: elfinder_url,
        title: 'elFinder 2.0',
        width: 900,
        height: 450,
        resizable: 'yes',
        inline: 'yes',
        popup_css: false,
        close_previous: 'no'
      }, {
        window: win,
        input: field_name
      });
      return false;
    }
  2. Integrate elFinder with Glide for image processing

    master

    To use Glide for generating thumbnails and managing image URLs in elFinder, follow these steps:

    1. Ensure your disk is configured in config/filesystems.php.
    2. Create a Glide Server route in your routes/web.php that uses the disk's driver and a cache path.
    3. Add the disk to your config/elfinder.php and provide the glideURL.

    Example Glide route implementation:

    Route::get('glide/{path}', function($path){
        $server = \League\Glide\ServerFactory::create([
            'source' => app('filesystem')->disk('public')->getDriver(),
            'cache' => storage_path('glide'),
        ]);
        return $server->getImageResponse($path, Input::query());
    })->where('path', '.+');

    Example elFinder config:

    'disks' => [
        'public' => [
            'glideURL' => '/glide',
        ],
    ],
  3. Integrate elFinder with TinyMCE 4.x

    master

    Use the elfinder.tinymce4 route. In your TinyMCE initialization, set file_browser_callback to a function that opens the elFinder URL using tinymce.activeEditor.windowManager.open.

    // TinyMCE init
    file_browser_callback : elFinderBrowser
    
    // Implementation
    function elFinderBrowser (field_name, url, type, win) {
      tinymce.activeEditor.windowManager.open({
        file: '<?= route('elfinder.tinymce4') ?>',
        title: 'elFinder 2.0',
        width: 900,
        height: 450,
        resizable: 'yes'
      }, {
        setUrl: function (url) {
          win.document.getElementById(field_name).value = url;
        }
      });
      return false;
    }
  4. Integrate elFinder with TinyMCE 5.x

    master

    To use elFinder with TinyMCE 5.x, use the elfinder.tinymce5 route. In your TinyMCE initialization, set file_picker_callback to a custom function that opens the elFinder URL in a window manager and handles the fileSelected action.

    // TinyMCE init
    file_picker_callback : elFinderBrowser
    
    // Implementation
    function elFinderBrowser (callback, value, meta) {
        tinymce.activeEditor.windowManager.openUrl({
            title: 'File Manager',
            url: elfinder_url,
            onMessage: function (dialogApi, details) {
                if (details.mceAction === 'fileSelected') {
                    const file = details.data.file;
                    const info = file.name;
                    if (meta.filetype === 'file') {
                        callback(file.url, {text: info, title: info});
                    } else if (meta.filetype === 'image') {
                        callback(file.url, {alt: info});
                    } else if (meta.filetype === 'media') {
                        callback(file.url);
                    }
                    dialogApi.close();
                }
            }
        });
    }
  5. Use elFinder as a Standalone Popup Selector

    master

    You can trigger elFinder in a popup window to populate an input field. This guide assumes the use of jQuery Colorbox (not included in this package).

    1. Setup Routes and Assets

    • Use the Barryvdh\Elfinder\ElfinderController@showPopup action for the popup.
    • Include Colorbox CSS in your <head>: <link href="/assets/css/colorbox.css" rel="stylesheet">
    • Include jQuery and Colorbox JS in your page.
    • Include the package's standalone popup script before the closing </body> tag: <script type="text/javascript" src="/packages/barryvdh/elfinder/js/standalonepopup.min.js"></script>

    2. Implementation

    • The target input must have a unique id.
    • The trigger element (link or button) must have the class popup_selector and a data-inputid attribute matching the input's ID.
    <!-- The target input -->
    <label for="feature_image">Feature Image</label>
    <input type="text" id="feature_image" name="feature_image" value="">
    
    <!-- The trigger element -->
    <a href="" class="popup_selector" data-inputid="feature_image">Select Image</a>
  6. Install elFinder for Laravel

    master

    To install the package, require it via Composer. After installation, you must publish the assets to your public folder using the dedicated artisan command. It is recommended to run this command after every update or include it in your composer.json post-update hooks.

    Note: For Laravel 8.x and older, use the latest 0.4 version of this package.

    composer require barryvdh/laravel-elfinder
    php artisan elfinder:publish
  7. Configure elFinder settings

    master

    By default, elFinder expects a files directory in your public folder. To customize the default folder, the access callback, or define custom roots, publish the configuration file to config/elfinder.php.

    php artisan vendor:publish --provider='Barryvdh\Elfinder\ElfinderServiceProvider' --tag=config
  8. Use Laravel Filesystem disks in elFinder

    master

    You can integrate Laravel's Flysystem-based disks (local or cloud) into elFinder using the disks configuration key. You can provide a simple string for a standard disk or an array to include extra options like URL and alias.

    'disks' => [
        'local',
        'my-disk' => [
            'URL' => url('to/disk'),
            'alias' => 'Local storage',
        ]
    ],
  9. Configure elFinder roots and disks

    master

    The showConnector method automatically builds the elFinder configuration based on your Laravel configuration. You can define file access via two primary methods:

    1. Local Directories

    Use the elfinder.dir configuration key to specify directories within your public path. For each directory, the package automatically sets the driver to LocalFileSystem and generates the appropriate URL.

    2. Laravel Filesystem Disks

    Use the elfinder.disks configuration key to map elFinder roots to Laravel filesystem disks. This allows you to use Flysystem drivers (like S3, FTP, etc.).

    When using disks, the package automatically configures the Flysystem driver and provides a URLCallback using the disk's url() method if a URL is not explicitly provided.

    Root Configuration Options

    Each root can be customized with:

    • driver: The driver used (e.g., LocalFileSystem or Flysystem).
    • path: The absolute path to the files.
    • URL: The base URL for the files.
    • accessControl: An optional filter callback (defined in elfinder.access).
    • URLCallback: A callback used to resolve file URLs (primarily for Flysystem disks).
    • alias: An alias for the disk.

    You can also apply global options to all roots using the elfinder.root_options configuration key.

  10. Configure elFinder routes

    master
    The package automatically registers a group of routes for the elFinder interface, connector, and various editor integrations (TinyMCE, CKEditor). These routes are wrapped in a group defined by the elfinder.route configuration key. You can customize the route prefix, middleware, or other group attributes by modifying the route array in your config/elfinder.php file.