php set medias,laravel5.1 -- Integrate FileManager and CKeditor into laravel
FileManager中文名叫文件管理器,也叫文件瀏覽器,它給我們提供了一個可視化的界面來管理文件和文件夾。利用FileManager,我們可以對文件進行瀏覽、增加、打印、修改(文件屬性)、重命名、搜索等等一大堆非常有用的操作。
CKeditor相信朋友們都非常熟悉了,它是一種富文本編輯器,不再贅述。
現在我們來演示如何將FileManager和CKeditor整合到laravel中去。
Install FileManager
Require filemanater
將filemanager加入到composer.json中我們用bestmomo/filemanager
require : {
"laravel/framework": "5.2.*",
"bestmomo/filemanager": "1.1.*"
}
Update Composer
$ composer update
更新完成后,將service provider加入到config/app.php中
/**
* App/Config/App.php
*/
Bestmomo\Filemanager\FilemanagerServiceProvider::class,
發布
$ php artisan vendor:publish --provider="Bestmomo\Filemanager\FilemanagerServiceProvider"
在User模型中添加2個權限方法
/**
* 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';
}
添加路由與方法
模型配置完了,就需要添加路由和控制器的方法了
路由
// route.php
Route::get('medias', ['as'=>'medias', 'uses'=>'Admin\AdminController@filemanager']);
配置文件
新建配置文件medias.php來配置引入的filemanager目錄
// Config/medias.php
return [
/*
|--------------------------------------------------------------------------
| Url for filemanager
|--------------------------------------------------------------------------
*/
'url' => 'filemanager/index.html',
'url-files' =>'/public/filemanager/userfiles/'
];
方法
在控制器AdminController中我們添加filemanager方法
/**
* 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模板
@extends('backend.layout.master')
@section('head')
.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;
}
@stop
@section('main')
@include('backend.partials.entete', ['heading' => trans('backend/medias.dashboard'), 'operation'=>'', 'symbol' => 'file-image-o', 'superior' => trans('backend/medias.medias')])
@stop
到了這里,整個FileManager就整合到了laravel中,但我在實際運行中,報了一個小錯誤:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Kbwebs\MultiAuth\Guard' does not have a method 'accessMediasAll'
說是'Kbwebs\MultiAuth\Guard'沒有accessMediasAll方法。
原因是我在laravel5.1中做了多用戶驗證功能,安裝了'Kbwebs\MultiAuth\Guard'插件,所以在所有要獲取User模型的時候都要加上一個user(), 比如 Auth::user->user(), auth()->user()->user()
解決方法:
找到有accessMediaAll和accessMediasFolder的文件/filemanager/connectors/php/default.config.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
* @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;
}
}
分別將
$app['auth']->user()->accessMediasAll()
$app['auth']->user()
$app['auth']->user()->accessMediasFolder()
$app['auth']->id()
改為
$app['auth']->user()->user()->accessMediasAll()
$app['auth']->user()->user()
$app['auth']->user()->user()->accessMediasFolder()
$app['auth']->user()->id()
FileManager界面
上面的代碼更新完成后,已經可以看到FileManager的文件管理界面
引入CKeditor
下載CKeditor并解壓到public文件夾后,在頁面中引入ckeditor/ckeditor.js
CKEDITOR.replace( 'summary');
config['height'] = 400;
CKEDITOR.replace( 'content');
如果要實現ckeditor從FileManager中導入文件,則需要修改filebrowserBrowseUrl選項
var config = {
filebrowserBrowseUrl : '/filemanager/index.html'
}
CKEDITOR.replace( 'summary', config);
config['height'] = 400;
CKEDITOR.replace( 'content', config);
ckeditor上傳圖片的效果圖
總結
以上是生活随笔為你收集整理的php set medias,laravel5.1 -- Integrate FileManager and CKeditor into laravel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抖音小黄鸭是谁画的呢?
- 下一篇: 在无痛人流多少钱啊?