laravel-admin cke编辑器安装和上传图片

admin 3个月前 190 阅读

[TOC]

安装

composer require laravel-admin-ext/ckeditor
php artisan vendor:publish --tag=laravel-admin-ckeditor

配置

config/admin.php 最下面的extensions添加

'extensions' => [
        'ckeditor' => [
        'enable' => true,
        'config' => [
            'filebrowserImageUploadUrl' => '/ckeditor/upload', // your url 
        ]
    ]
],

创建路由

Route::post('ckeditor/upload', [App\Http\Controllers\CkeditorUploadController::class, 'uploadImage'])->name('uploadImage'); 

创建控制器

CkeditorUploadController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CkeditorUploadController extends Controller
{
    public function uploadImage(Request $request)
    {
        $image = request()->file('upload');
        //保存到本地或者选定特定的 disk
        $url = \Storage::disk('public')->put('images',$image);
        $param = [
            'uploaded' => 1,
            'fileName' => 'fileName',
            'url' => '/uploads/'.$url
        ];

        return response()->json($param, 200); 
    }
}

设置 public 保存地址

因为懒得设置软链接,习惯将 public/uploads 作为公共的图片上传目录
config/filesystems.php 里 disks 的 public 部分改为

'public' => [
    'driver' => 'local',
    'root' => public_path('uploads'),
    'url' => '/uploads',
    'visibility' => 'public',
],

后台表单

$form->ckeditor('description')->options([
    'lang' => 'fr',
    'height' => 500,
    'filebrowserImageUploadUrl' => config('admin.extensions.ckeditor.config.filebrowserImageUploadUrl').'?_token='.csrf_token(),
]);

end;

回复 0
登录之后才能参与