News

Featured 27 Mar
Laravel File Manager
Laravel File Manager

Laravel File Manager Integration

In this tutorial we will go over the setup of a useful Laravel UniSharp File Manager for content areas that are used mostly for custom Content Management Systems, or any application that may need a text editor to perform common tasks and file image uploading, these days nobody uses just plain text area for an application, unless its used for a meta description field or short summary, for content it is better to use a featured WYSIWYG editor to provide a better user experience. We will be looking at a fully working demo application at Task MGMT that you can try and see how the final result, also discuss how you can achieve the same for your own personal projects.

Note: I will assume you already installed Laravel, can run php artisan commands and have a working Laravel installation. The running demo is using Laravel 5.5

File Manager Requirements

  • php >= 5.4
  • exif extension
  • fileinfo extension
  • GD Library >=2.0 or Imagick PHP extension >=6.5.7
  • Laravel 5
  • requires intervention/image (to make thumbs, crop and resize images).

File Manager Installation

Open up your terminal or command prompt and enter in the root of your project directory:
Step 1: install package

1
composer require unisharp/laravel-filemanager:~1.8

If you are running Laravel 5.5, ignore step 2 below, and go to step 3 instead.
Step 2: add to providers and aliases arrays
Once the package is installed open config/app.php and add these 2 lines to the end of the providers array

1
2
3
4
5
'providers' => [
    ...    
    UnisharpLaravelfilemanagerLaravelFilemanagerServiceProvider::class,
    InterventionImageImageServiceProvider::class,
],

Next, in the same config/app.php file, add the following line in the aliases array:

1
2
3
4
'aliases' => [
    ... 
    'Image' => InterventionImageFacadesImage::class,
],

Step 3: Publish and clear package
Enter the following php artisan commands

1
2
3
4
php artisan vendor:publish --tag=lfm_config
php artisan vendor:publish --tag=lfm_public
php artisan route:clear
php artisan config:clear

Step 4: Config/Create directories
Open config/lfm.php
Note the following:

1
2
3
4
5
6
7
    'base_directory' => 'public',
 
    'images_folder_name' => 'photos',
    'files_folder_name'  => 'files',
 
    'shared_folder_name' => 'shares',
    'thumb_folder_name'  => 'thumbs',

Ensure these are writable by your web server

WYSIWYG Editor Integration

Now that we have the required packages, lets make use of this File Manager, i will be using TinyMCE4, you can find information on how to setup other WYSIWYG editors such as CKEditor, Summernote.

In order to focus on the most important aspects of this topic, certain parts will be removed, fields that we dont need so we concentrate only on a few fields, with the content editor being the most important one.
The full source code can be found here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@extends('layout')
 
@section('content')
 
@include('includes.errors') 
 
<form id="task_form" action="{{ route('task.store') }}" method="POST" enctype="multipart/form-data">
    {{ csrf_field() }}
 
    <div class="col-md-12">
        <label>Create new task <span class="glyphicon glyphicon-plus" aria-hidden="true"></span></label>
 
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Enter Task Title" name="task_title">
        </div>
 
        <div class="form-group">
            <textarea class="form-control my-editor" rows="10" id="task" name="task"></textarea>
        </div>
 
        <div class="btn-group">
            <input class="btn btn-primary" type="submit" value="Submit">
            <a class="btn btn-default" href="{{ redirect()->getUrlGenerator()->previous() }}">Back</a>
        </div>
 
    </div>
 
</form>
 
@stop
 
 
@section('scripts') 
 
    <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
 
    <script>
      var editor_config = {
        // path_absolute : "/",
        path_absolute:"{{ url('/') }}/",
 
        selector: "textarea.my-editor",
        plugins: [
          "advlist autolink lists link image charmap print preview hr anchor pagebreak",
          "searchreplace wordcount visualblocks visualchars code fullscreen",
          "insertdatetime media nonbreaking save table contextmenu directionality",
          "emoticons template paste textcolor colorpicker textpattern"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media",
        relative_urls: false,
        file_browser_callback : function(field_name, url, type, win) {
          var x = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
          var y = window.innerHeight|| document.documentElement.clientHeight|| document.getElementsByTagName('body')[0].clientHeight;
 
          var cmsURL = editor_config.path_absolute + 'laravel-filemanager?field_name=' + field_name;
          if (type == 'image') {
            cmsURL = cmsURL + "&type=Images";
          } else {
            cmsURL = cmsURL + "&type=Files";
          }
 
          tinyMCE.activeEditor.windowManager.open({
            file : cmsURL,
            title : 'Filemanager',
            width : x * 0.8,
            height : y * 0.8,
            resizable : "yes",
            close_previous : "no"
          });
        } ,
 
        //  Add Bootstrap Image Responsive class for inserted images
        image_class_list: [
            {title: 'None', value: ''},
            {title: 'Bootstrap responsive image', value: 'img-responsive'},
        ]   
 
      };
 
      tinymce.init(editor_config);
    </script>
 
@stop

Lets examine the important points from this script

First you need to load tinycme JS

1
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>

– In our editor_config JS code we have a property called “selector” where we define the class name for our textarea: my-editor, and the html:

1
<textarea class="form-control my-editor" rows="10" id="task" name="task"></textarea>

Also in editor_config we set the path_absolute property with using a Laravel url helper: {{ url(/)}} to print dynamically the url of the site.

Adding custom classes for images

Lets say you would like tinyMCE to add a dropdown to add a custom class to the image that was just selected, you can do so by adding an additional property to the editor_config options

1
2
3
4
5
6
7
8
9
10
11
12
13
  var editor_config = {
    //path_absolute : "/",
    path_absolute:"{{ url('/') }}/",
 
    // ...
 
    //  Add Bootstrap Image Responsive class for inserted images
    image_class_list: [
        {title: 'None', value: ''},
        {title: 'Bootstrap responsive image', value: 'img-responsive'},
    ]
 
  };

What this will do is add a dropdown option to add an image class to the selected image.

Why is this Laravel File Manager Package useful?

You might ask yourself why do we need to do all of these steps, we could just use any other WYSIWYG editor, here are a couple of reasons why:
1) You can use any other WYSIWYG editor but the problem is when you actually select the image, if you go into source code view you will see the image inserted as:

1
<img src="data:image/png;base64,iVBO0KGgoAAKGgoAAAANSUhiVBO0KGgoAAAANSUhEURw0KGgoAAiVBO0KGgoAAAAA0iVBO0KGgoAAAAKGgoAAAANSUhiVBO0KGgoAAAAEUANSUhEUgAAADIA..." />

the actual base64 encoding will be a lot longer than this depending on the size of the image, this means you will be storing a huge string in the database.

In our version with the help of this Laravel File Manager if you go into source code view you will see images as:

1
<img src="images/cat.jpg">

Laravel

Readmore
22 Aug
Laravel
Laravel

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Image result for ionic

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Image result for angularjs

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Image result for ngcordova

Readmore