Home > Backend Development > PHP Tutorial > Solution to the problem of image list display in laravel-admin integrated ueditor editor

Solution to the problem of image list display in laravel-admin integrated ueditor editor

不言
Release: 2023-04-02 18:32:02
Original
3392 people have browsed it

This article mainly introduces the solution to the picture list display problem of laravel-admin integrated ueditor editor. It has a certain reference value. Now I share it with you. Friends in need can refer to it

laravel -Admin integrates ueditor and it is recommended to use laravel-u-editor, which is the laravel composer version of ueditor. Developed based on UEditor 1.4.3.3, supports en, zh_CN, zh_TW, and supports local and Qiniu cloud storage. The default is local upload public/uploads

Installation

composer require stevenyangecho/laravel-u-editor
然后在config/app.php的providers下增加一行
Stevenyangecho\UEditor\UEditorServiceProvider::class
执行
php artisan vendor:publish
Copy after login

Basic configuration

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 = &#39;admin.uEditor&#39;;

    // css资源
    protected static $css = [];

    // js资源
    protected static $js = [
        &#39;laravel-u-editor/ueditor.config.js&#39;,
        &#39;laravel-u-editor/ueditor.all.min.js&#39;,
        &#39;laravel-u-editor/lang/zh-cn/zh-cn.js&#39;
    ];

    public function render()
    {
        $cs=csrf_token();
        $this->script = <<<EOT
        //解决第二次进入加载不出来的问题
        UE.delEditor("ueditor");
        // 默认id是ueditor
        var ue = UE.getEditor(&#39;ueditor&#39;); 
        ue.ready(function () {
            ue.execCommand(&#39;serverparam&#39;, &#39;_token&#39;, &#39;$cs&#39;);
        });

EOT;
        return parent::render();
    }
}



2. 增加视图文件: resources/views/admin/uEditor.blade.php

<p class="form-group {!! !$errors->has($errorKey) ?: &#39;has-error&#39; !!}">
    <label for="{{$id}}" class="col-sm-2 control-label">{{$label}}</label>
    <p class="col-sm-8">
        @include(&#39;admin::form.error&#39;)
        {{-- 这个style可以限制他的高度,不会随着内容变长 --}}
        <textarea type=&#39;text/plain&#39; style="height:400px;" id=&#39;ueditor&#39; id="{{$id}}" name="{{$name}}" placeholder="{{ $placeholder }}" {!! $attributes !!}  class=&#39;ueditor&#39;>
            {!! old($column, $value) !!}
        </textarea>
        @include(&#39;admin::form.help-block&#39;)
    </p>
</p>

3. 然后注册进laravel-admin,在app/Admin/bootstrap.php中添加以下代码:

use App\Admin\Extensions\Form\uEditor;
Encore\Admin\Form::extend(&#39;ueditor&#39;, uEditor::class);

4. 调用:
$form->ueditor(&#39;content&#39;, &#39;内容&#39;)->rules(&#39;required&#39;);
Copy after login

Configuring Qiniu Service

打开文件config\UEditorUpload.php配置七牛配置参数

// 将下一行注释
//        &#39;mode&#39;=>&#39;local&#39;,//上传方式,local 为本地   qiniu 为七牛
        &#39;mode&#39;=>&#39;qiniu&#39;,//上传方式,local 为本地   qiniu 为七牛

        //七牛配置,若mode=&#39;qiniu&#39;,以下为必填.
        &#39;qiniu&#39;=>[
            &#39;accessKey&#39;=>&#39;。。。&#39;,
            &#39;secretKey&#39;=>&#39;。。。&#39;,
            &#39;bucket&#39;=>&#39;。。。&#39;,
            &#39;url&#39;=>&#39;。。。&#39;,//七牛分配的CDN域名,注意带上http://

        ]
    ],
Copy after login

Fix the problem that the file list cannot be managed in the upload interface

vendor\stevenyangecho\laravel-u-editor\src\ListsQiniu.php
public function getList()
    {
        $size = $this->request->get(&#39;size&#39;, $this->listSize);
        $start = $this->request->get(&#39;start&#39;, &#39;&#39;);
        $auth = new Auth(config(&#39;UEditorUpload.core.qiniu.accessKey&#39;), config(&#39;UEditorUpload.core.qiniu.secretKey&#39;));

        $bucketManager = new BucketManager($auth);
        // 注释掉下面的行
//        list($items, $marker, $error) = $bucketManager->listFiles(config(&#39;UEditorUpload.core.qiniu.bucket&#39;), $this->path, $start, $size);
// 下面三行是修改的
        $result1 = $bucketManager->listFiles(config(&#39;UEditorUpload.core.qiniu.bucket&#39;), $this->path, $start, $size);
        $size = count($result1);
        // 如果不取到items,报找不到key的错误
        $items = $result1[0][&#39;items&#39;];
// 判断是否错误
        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[&#39;items&#39;]);
        foreach ($items as  $v) {
            if (preg_match("/\.(" . $this->allowFiles . ")$/i", $v[&#39;key&#39;])) {
                $files[] = array(
                    &#39;url&#39; =>rtrim(config(&#39;UEditorUpload.core.qiniu.url&#39;),&#39;/&#39;).&#39;/&#39;.$v[&#39;key&#39;],
                    &#39;mtime&#39; => $v[&#39;mimeType&#39;],
                );
            }
        }
        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;
    }
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's learning, more related Please pay attention to the PHP Chinese website for content!

Related recommendations:

Solution to the problem that select in laravel-admin cannot automatically select the current value when editing the form

gitbash PHP execution output Chinese random coding solution

The above is the detailed content of Solution to the problem of image list display in laravel-admin integrated ueditor editor. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template