标签:style 安装 配置 source ext img har too 时间
文章数据表主要有4个字段
创建文章模型,不要忘记设置验证规则和字段的名称
namespace backend\models; class Article extends \yii\db\ActiveRecord {
public function rules()
{
return [
[[‘title‘, ‘content‘], ‘required‘],
];
}
public function attributeLabels()
{
return [
‘id‘ => ‘ID‘,
‘title‘ => ‘名称‘,
‘content‘ => ‘内容‘,
];
}
}
创建文章控制器并编写发布文章功能
namespace backend\controllers; use backend\models\Article; class ArticleController extends \yii\web\Controller { /* * 发布文章 */ public function actionAdd() { $article = new Article(); if($article->load(\Yii::$app->request->post()) && $article->validate()){
$article->created_time = time(); $article->save(); \Yii::$app->session->setFlash(‘success‘,‘文章添加成功‘); return $this->refresh(); } return $this->render(‘add‘,[‘article‘=>$article]); }
}
使用composer命令安装
composer require kucha/ueditor "*"
在控制器中定义动作,用于处理UEditor上传的文件。
可以配置域名,上传路径,上传文件命名格式等等
public function actions() { return [ ‘upload‘ => [ ‘class‘ => ‘kucha\ueditor\UEditorAction‘, ‘config‘ => [ "imageUrlPrefix" => "",//图片访问路径前缀 "imagePathFormat" => "/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}" //上传保存路径 "imageRoot" => Yii::getAlias("@webroot"), ], ] ]; }
在视图表单中使用如下代码显示UEditor编辑器
$form = \yii\bootstrap\ActiveForm::begin(); echo $form->field($article,‘title‘); echo $form->field($article,‘content‘)->widget(‘kucha\ueditor\UEditor‘,[ ‘clientOptions‘ => [ //编辑区域大小 ‘initialFrameHeight‘ => ‘200‘, //设置语言 ‘lang‘ =>‘en‘, //中文为 zh-cn //定制菜单 ‘toolbars‘ => [ [ ‘fullscreen‘, ‘source‘, ‘undo‘, ‘redo‘, ‘|‘, ‘fontsize‘, ‘bold‘, ‘italic‘, ‘underline‘, ‘fontborder‘, ‘strikethrough‘, ‘removeformat‘, ‘formatmatch‘, ‘autotypeset‘, ‘blockquote‘, ‘pasteplain‘, ‘|‘, ‘forecolor‘, ‘backcolor‘, ‘|‘, ‘lineheight‘, ‘|‘, ‘indent‘, ‘|‘ ], ] ]); echo \yii\bootstrap\Html::submitButton(‘提交‘,[‘class‘=>‘btn btn-info‘]); \yii\bootstrap\ActiveForm::end();
以下是发布文章功能编写完成后的效果,是不是很炫?
标签:style 安装 配置 source ext img har too 时间
原文地址:http://www.cnblogs.com/felixji/p/6698436.html