詳解在YII2框架中使用UEditor編輯器發(fā)布文章
本文介紹了詳解在YII2框架中使用UEditor編輯器發(fā)布文章 ,分享給大家,具體如下:
創(chuàng)建文章數(shù)據(jù)表
文章數(shù)據(jù)表主要有4個字段
1.id 主鍵(int)
2.title 標題(varchar)
3.content 內(nèi)容(text)
4.created_time 創(chuàng)建時間(int)
創(chuàng)建文章模型
創(chuàng)建文章模型,不要忘記設置驗證規(guī)則和字段的名稱
namespace backend\models;
class Article extends \yii\db\ActiveRecord
{
public function rules()
{
return [
[['title', 'content'], 'required'],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => '名稱',
'content' => '內(nèi)容',
];
}
}
創(chuàng)建控制器
創(chuàng)建文章控制器并編寫發(fā)布文章功能
namespace backend\controllers;
use backend\models\Article;
class ArticleController extends \yii\web\Controller
{
/*
* 發(fā)布文章
*/
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]);
}
}
安裝UEditor小部件
使用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編輯器
在視圖表單中使用如下代碼顯示UEditor編輯器
$form = \yii\bootstrap\ActiveForm::begin();
echo $form->field($article,'title');
echo $form->field($article,'content')->widget('kucha\ueditor\UEditor',[
'clientOptions' => [
//編輯區(qū)域大小
'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();
最終頁面效果

以下是發(fā)布文章功能編寫完成后的效果,是不是很炫?希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
反射調(diào)用private方法實踐(php、java)
這篇文章主要介紹了反射調(diào)用private方法實踐(php、java)的相關資料,需要的朋友可以參考下2015-12-12
Laravel中GraphQL接口請求頻率實戰(zhàn)記錄
這篇文章主要給大家介紹了關于Laravel中GraphQL接口請求頻率的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
Thinkphp搭建包括JS多語言的多語言項目實現(xiàn)方法
這篇文章主要介紹了Thinkphp搭建包括JS多語言的多語言項目實現(xiàn)方法,可實現(xiàn)通過針對js語言包的調(diào)用達到構建多語言站點的效果,是非常實用的技巧,需要的朋友可以參考下2014-11-11
thinkphp實現(xiàn)面包屑導航(當前位置)例子分享
今天把博客一些細節(jié)完善了一下,其中修改了一下欄目頁和文章頁中的“當前位置”。2014-05-05

