时间:2021-07-01 10:21:17 帮助过:6人阅读
laravel-admin集成ueditor推荐使用laravel-u-editor,它是ueditor的laravel composer版本。基于UEditor 1.4.3.3开发,支持en、zh_CN、zh_TW,并且支持本地和七牛云存储,默认为本地上传 public/uploads
composer require stevenyangecho/laravel-u-editor 然后在config/app.php的providers下增加一行 Stevenyangecho\UEditor\UEditorServiceProvider::class 执行 php artisan vendor:publish
1.增加组件文件:app/Admin/Extensions/Form/uEditor.php: <?php namespace App\Admin\Extensions\Form; use Encore\Admin\Form\Field; /** * 百度编辑器 * Class uEditor * @package App\Admin\Extensions\Form */ class uEditor extends Field { // 定义视图 protected $view = 'admin.uEditor'; // css资源 protected static $css = []; // js资源 protected static $js = [ 'laravel-u-editor/ueditor.config.js', 'laravel-u-editor/ueditor.all.min.js', 'laravel-u-editor/lang/zh-cn/zh-cn.js' ]; public function render() { $cs=csrf_token(); $this->script = <<<EOT //解决第二次进入加载不出来的问题 UE.delEditor("ueditor"); // 默认id是ueditor var ue = UE.getEditor('ueditor'); ue.ready(function () { ue.execCommand('serverparam', '_token', '$cs'); }); EOT; return parent::render(); } } 2. 增加视图文件: resources/views/admin/uEditor.blade.php <p class="form-group {!! !$errors->has($errorKey) ?: 'has-error' !!}"> <label for="{{$id}}" class="col-sm-2 control-label">{{$label}}</label> <p class="col-sm-8"> @include('admin::form.error') {{-- 这个style可以限制他的高度,不会随着内容变长 --}} <textarea type='text/plain' style="height:400px;" id='ueditor' id="{{$id}}" name="{{$name}}" placeholder="{{ $placeholder }}" {!! $attributes !!} class='ueditor'> {!! old($column, $value) !!} </textarea> @include('admin::form.help-block') </p> </p> 3. 然后注册进laravel-admin,在app/Admin/bootstrap.php中添加以下代码: use App\Admin\Extensions\Form\uEditor; Encore\Admin\Form::extend('ueditor', uEditor::class); 4. 调用: $form->ueditor('content', '内容')->rules('required');
打开文件config\UEditorUpload.php配置七牛配置参数 // 将下一行注释 // 'mode'=>'local',//上传方式,local 为本地 qiniu 为七牛 'mode'=>'qiniu',//上传方式,local 为本地 qiniu 为七牛 //七牛配置,若mode='qiniu',以下为必填. 'qiniu'=>[ 'accessKey'=>'。。。', 'secretKey'=>'。。。', 'bucket'=>'。。。', 'url'=>'。。。',//七牛分配的CDN域名,注意带上http:// ] ],
vendor\stevenyangecho\laravel-u-editor\src\ListsQiniu.php public function getList() { $size = $this->request->get('size', $this->listSize); $start = $this->request->get('start', ''); $auth = new Auth(config('UEditorUpload.core.qiniu.accessKey'), config('UEditorUpload.core.qiniu.secretKey')); $bucketManager = new BucketManager($auth); // 注释掉下面的行 // list($items, $marker, $error) = $bucketManager->listFiles(config('UEditorUpload.core.qiniu.bucket'), $this->path, $start, $size); // 下面三行是修改的 $result1 = $bucketManager->listFiles(config('UEditorUpload.core.qiniu.bucket'), $this->path, $start, $size); $size = count($result1); // 如果不取到items,报找不到key的错误 $items = $result1[0]['items']; // 判断是否错误 if ($size > 2) { return [ "state" => $error->message(), "list" => array(), "start" => $start, "total" => 0 ]; } if(empty($items)){ return [ "state" => "no match file", "list" => array(), "start" => $start, "total" => 0 ]; } $files=[]; // dd($items['items']); foreach ($items as $v) { if (preg_match("/\.(" . $this->allowFiles . ")$/i", $v['key'])) { $files[] = array( 'url' =>rtrim(config('UEditorUpload.core.qiniu.url'),'/').'/'.$v['key'], 'mtime' => $v['mimeType'], ); } } if(empty($files)){ return [ "state" => "no match file", "list" => array(), "start" => $start, "total" => 0 ]; } /* 返回数据 */ $result = [ "state" => "SUCCESS", "list" => $files, "start" => $start, "total" => count($files) ]; return $result; }
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
解决laravel-admin中select在form编辑时不能自动选中当前的值的问题
gitbash PHP执行输出中文乱编解决方式
以上就是laravel-admin集成ueditor编辑器的图片列表显示问题解决方法的详细内容,更多请关注Gxl网其它相关文章!