yii使用uedit编辑器
1.首先下载uedit编辑器文件,大致如下
2.自定义uedit.php文件
3.编辑ueditor.config.js 中的图片路径
4.设置图片访问前缀
5.uedit.php代码如下
<?php namespace backend\components; use yii\helpers\Html; use yii\helpers\Json; use yii\widgets\InputWidget; class Ueditor extends InputWidget { public $attributes; public function init() { parent::init(); } public function run() { $view = $this->getView(); $this->attributes['id']=$this->options['id']; if($this->hasModel()){ $input=Html::activeTextarea($this->model, $this->attribute,$this->attributes); }else{ $input=Html::textarea($this->name,'',$this->attributes); } echo $input; UeditorAsset::register($view);//将Ueditor用到的脚本资源输出到视图 $js='var ue = UE.getEditor("'.$this->options['id'].'",'.$this->getOptions().');';//Ueditor初始化脚本 $view->registerJs($js, $view::POS_END);//将Ueditor初始化脚本也响应到视图中 } public function getOptions() { unset($this->options['id']);//Ueditor识别不了id属性,故而删之 return Json::encode($this->options); } }
UeditorAsset.php
<?php namespace backend\components; use yii\web\AssetBundle; class UeditorAsset extends AssetBundle { public $js = [ 'ueditor.config.js', 'ueditor.all.js', ]; public $css = [ ]; public function init() { $this->sourcePath =$_SERVER['DOCUMENT_ROOT'].\Yii::getAlias('@web').'/js'; //设置资源所处的目录 } }
在视图中
<?= $form->field($model, 'duty')->widget(Ueditor::className(),[ 'options'=>[ 'focus'=>true, 'toolbars'=> [ ['fullscreen', 'source', 'undo', 'redo', 'bold'], [ 'anchor', //锚点 'undo', //撤销 'redo', //重做 'bold', //加粗 'indent', //首行缩进 'background', //背景 ] ], ], 'attributes'=>[ 'style'=>'height:80px' ] ]) ?>
记得引入use backend\components\Ueditor;