心灵艺术 2018-02-03 16:08:19 4575次浏览 1条评论 7 6 1

拿来主义,所以没跟踪代码的习惯,在网上找资料以及群里问人,哎,很悲哀网上很多复制粘贴的代码,以及在群里问不到问题,恶心到我半死,特地慢慢看这个狗屁膏药究竟是怎么弄。一步步来返璞归真,好了,废话不说了。
百度编辑器下载:http://ueditor.baidu.com (版本无所谓)
先来了解一下widgets的小知识,对下面有一定的了解,毕竟拿东西过来要知道怎么弄的情况吧。

简单粗暴的理解,就是widgets小部件就是类似(include 或 require)的意思,要用随时拿,好了,有这个思想,咱们继续往下走。
将下载好的百度编辑器解压目录(ueditor1_4_2-utf8-php/ueditor1_4_2-utf8-php)我下载的是这个版本ueditor1_4_2-utf8-php,解压好之后重命名了,目录改成ueditor/assets放到common/widgets目录中。
我们只要新建2个文件就可以全部搞定百度编辑器的载入工作。
新建文件:AppAsset.php这个主要用来注册JS的存放位置的。

<?php
    /**
     * failboy 42419962@qq.com
     */
    namespace common\widgets\ueditor;

    use yii\web\AssetBundle;

    class AppAsset extends AssetBundle
    {

        public $js = [
            'ueditor.config.js',
            'ueditor.all.min.js',
            'lang/zh-cn/zh-cn.js'
        ];

        public function init()
        {
            $this->sourcePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
        }

    }

继续新建文件:UploadAction.php

<?php
/**
 * failboy 42419962@qq.com
 */
namespace common\widgets\ueditor;

use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;

class UploadAction extends InputWidget
{
    //百度编辑器配置选项
    public $options = [];

    public function run()
    {
        $this->registerScript();
        if ($this->hasModel()) {
            return Html::activeTextarea($this->model, $this->attribute, ['id' => $this->id]);
        } else {
            return Html::textarea($this->id, $this->value, ['id' => $this->id]);
        }
    }

    /**
     * 载入JS脚本
     */
    protected function registerScript()
    {
        AppAsset::register($this->view);
        $getOptions = Json::encode($this->options);
        $script = "UE.getEditor('" . $this->id . "', " . $getOptions . ");";
        $this->view->registerJs($script);
    }
}

这样就弄好百度编辑器的载入。
前台调用方式:

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>

<div class="ceshi-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model,'内容字段')->widget('common\widgets\ueditor\UploadAction',['options'=>[]]);?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

这样就全部弄好了。有更好方法的朋友来一起探讨噢。

觉得很赞
  • 评论于 2018-05-31 22:24 举报

    点个赞,好用。
    不过有个小坑~
    我修复下:
    1.百度编辑器,下载后解压,最外面的文件夹改名为:ueditor
    2.AppAsset.php 文件中 init() 方法返回:$this->sourcePath = dirname(FILE) . DIRECTORY_SEPARATOR; 就好了。

    1 条回复
    评论于 2018-06-02 15:40 回复

    嗯嗯。也对。感谢修正。

您需要登录后才可以评论。登录 | 立即注册