laravel5.1 -- Integrate FileManager and CKeditor into laravel

Keywords: PHP Laravel Session JSON

FileManager Chinese name is File Manager, also known as File Browser, which provides us with a visual interface to manage files and folders. With FileManager, we can browse, add, print, modify (file attributes), rename, search and so on a lot of very useful operations.
CKeditor believes that friends are very familiar with it. It's a rich text editor, not to mention it any more.
Now let's demonstrate how to integrate FileManager and Keditor into laravel.

Install FileManager

Require filemanater

To add filemanager to composer.json, we use bestmomo/filemanager

require : {
        "laravel/framework": "5.2.*",
        "bestmomo/filemanager": "1.1.*"
    }

Update Composer

$ composer update

After the update is complete, add the service provider to config/app.php

 /**
 * App/Config/App.php
 */

Bestmomo\Filemanager\FilemanagerServiceProvider::class,

Release

$ php artisan vendor:publish --provider="Bestmomo\Filemanager\FilemanagerServiceProvider"

Adding two permission methods to User model

/**
* App/Http/Models/User.php
*/

    /**
    * Check media all access
    *
    * @return bool
    */
    public function accessMediasAll(){
    
        return $this->role->slug == 'admin';
    }

    /**
     * Check media access one folder
     *
     * @return bool
     */
    public function accessMediasFolder()
    {
        return $this->role->slug != 'user';
    }

Adding routing and methods

When the model is configured, you need to add routers and controllers.

Route

// route.php

Route::get('medias', ['as'=>'medias', 'uses'=>'Admin\AdminController@filemanager']);

configuration file

New configuration file medias.php to configure the imported filemanager directory

// Config/medias.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Url for filemanager
    |--------------------------------------------------------------------------
    */

    'url' => 'filemanager/index.html',
    'url-files' =>'/public/filemanager/userfiles/'

];

Method

In the controller AdminController, we add the filemanager method

    /**
    * Show the media panel
    *
    * @return Response
    */
    public function filemanager(){
        $url = config('medias.url') . '?langCode=' . config('app.locale');

        return view('backend.filemanager')->with(compact('url'));
    }

filemanager.blade.php template

@extends('backend.layout.master')

@section('head')

    <style type="text/css">

        .iframe-responsive-wrapper {
            position: relative;
        }

        .iframe-responsive-wrapper .iframe-ratio {
            display: block;
            width: 100%;
            height: auto;
        }

        .iframe-responsive-wrapper iframe {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
        
        #page-wrapper {
            background-color: #222;
        }

        .page-header {
            color: #ddd;
        }

    </style>

@stop

@section('main')

    @include('backend.partials.entete', ['heading' => trans('backend/medias.dashboard'), 'operation'=>'', 'symbol' => 'file-image-o', 'superior' => trans('backend/medias.medias')])

    <div class="iframe-responsive-wrapper">
        <img class="iframe-ratio" src="data:image/gif;base64,R0lGODlhEAAJAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAkAAAIKhI+py+0Po5yUFQA7"/>
        <iframe scrolling="no" src="{!! url($url) !!}" width="640" height="360" frameborder="2" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
    </div>

@stop

At this point, the entire FileManager is integrated into laravel, but in actual operation, I reported a minor error:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Kbwebs\MultiAuth\Guard' does not have a method 'accessMediasAll'
Say'Kbwebs MultiAuth Guard'has no access MediasAll method.
The reason is that I did multi-User authentication in laravel5.1 and installed the'Kbwebs\MultiAuth\Guard'plug-in, so we need to add a user(), such as Auth::User-> user(), auth ()-> user()-> user() when we want to get the User model.

Solution:

Find the file / filemanager/connectors/php/default.config.php with accessMediaAll and accessMediasFolder

<?php
/**
 *  Filemanager PHP connector
 *  This file should at least declare auth() function 
 *  and instantiate the Filemanager as '$fm'
 *  
 *  IMPORTANT : by default Read and Write access is granted to everyone
 *  Copy/paste this file to 'user.config.php' file to implement your own auth() function
 *  to grant access to wanted users only
 *
 *  filemanager.php
 *  use for ckeditor filemanager
 *
 *  @license  MIT License
 *  @author   Simon Georget <simon (at) linea21 (dot) com>
 *  @copyright  Authors
 */

// Laravel init
require getcwd() . '/../../../../bootstrap/autoload.php';
$app = require_once getcwd() . '/../../../../bootstrap/app.php';

$kernel = $app->make('Illuminate\Contracts\Http\Kernel');

$response = $kernel->handle(
  $request = Illuminate\Http\Request::capture()
);

$id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]);
$app['session']->driver()->setId($id);
$app['session']->driver()->start();

// Folder path
$folderPath = config('filemanager.folder_path');   

// Check if user in authentified
if(!$app['auth']->check()) 
{
  $laravelAuth = false;
} 
else 
{ //print_r($app['auth']->user()->user()->accessMediasAll());exit;
  // Check if user has all access
  if($app['auth']->user()->accessMediasAll())
  {
    $laravelAuth = true; 
  } 
  elseif(method_exists($app['auth']->user(), 'accessMediasFolder'))
  {  
    // Check if user has access to one folder
    if($app['auth']->user()->accessMediasFolder())
    { 
      // Folder name with user id
      $folderPath .= 'user' . $app['auth']->id();
      $laravelAuth = true;  
    } 
    else
    {
      $laravelAuth = false;
    }    
  }
  else
  {
    $laravelAuth = false;
  } 
}

Separately
$app['auth']->user()->accessMediasAll()
$app['auth']->user()
$app['auth']->user()->accessMediasFolder()
$app['auth']->id()
Change to
$app['auth']->user()->user()->accessMediasAll()
$app['auth']->user()->user()
$app['auth']->user()->user()->accessMediasFolder()
$app['auth']->user()->id()

FileManager interface

After the above code update is completed, you can see FileManager's file management interface

Posted by beckjoh on Sat, 22 Dec 2018 23:48:06 -0800