2019-04-25 10:25:02 2694瀏覽
本篇文章扣丁學(xué)堂PHP培訓(xùn)小編給讀者們詳解一下如何在YII2框架中使用UEditor編輯器發(fā)布文章,對PHP開發(fā)技術(shù)感興趣或者是想要參加PHP培訓(xùn)學(xué)習(xí)的小伙伴們就隨小編來了解一下吧。
現(xiàn)在將YII2框架中使用UEditor編輯器發(fā)布文章分享給大家,具體如下:
創(chuàng)建文章數(shù)據(jù)表
文章數(shù)據(jù)表主要有4個字段
1、id 主鍵(int)
2、title 標(biāo)題(varchar)
3、content 內(nèi)容(text)
4、created_time 創(chuàng)建時間(int)
創(chuàng)建文章模型
創(chuàng)建文章模型,不要忘記設(shè)置驗證規(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', //設(shè)置語言 '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();
最終頁面效果:
想要了解更多關(guān)于PHP開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂PHP培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的PHP講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開發(fā)實戰(zhàn)技能??鄱W(xué)堂PHP技術(shù)交流群:374332265。
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】 【掃碼進入Python全棧開發(fā)免費公開課】
查看更多關(guān)于“php培訓(xùn)資訊”的相關(guān)文章>>