詳解在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ā)布文章功能編寫完成后的效果,是不是很炫?希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
淺談laravel-admin的sortable和orderby使用問題
今天小編就為大家分享一篇淺談laravel-admin的sortable和orderby使用問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10thinkPHP數(shù)據(jù)查詢常用方法總結【select,find,getField,query】
這篇文章主要介紹了thinkPHP數(shù)據(jù)查詢常用方法,結合實例形式總結分析了select,find,getField,query等方法進行數(shù)據(jù)庫查詢操作的具體操作步驟與相關實現(xiàn)技巧,需要的朋友可以參考下2017-03-03PHP中設置一個嚴格30分鐘過期Session面試題的4種答案
這篇文章主要介紹了PHP中設置一個嚴格30分鐘過期Session面試題的4種答案,需要的朋友可以參考下2014-07-07Zend Framework創(chuàng)建自己的動作助手詳解
這篇文章主要介紹了Zend Framework創(chuàng)建自己的動作助手實現(xiàn)方法,結合實例形式分析了基于助手的抽象基類Zend_Controller_Action_Helper_Abstract實現(xiàn)自定義動作助手的相關技巧,需要的朋友可以參考下2016-03-03php計算幾分鐘前、幾小時前、幾天前的幾個函數(shù)、類分享
這篇文章主要介紹了php計算時間幾分鐘前、幾小時前、幾天前的幾個函數(shù)、類分享,需要的朋友可以參考下2014-04-04TP5(thinkPHP5)框架mongodb擴展安裝及特殊操作示例
這篇文章主要介紹了TP5(thinkPHP5)框架mongodb擴展安裝及特殊操作,結合實例形式分析了MongoDB擴展的基本安裝、配置、模型操作以及使用Push操作實現(xiàn)的數(shù)據(jù)添加、更新等方法,需要的朋友可以參考下2018-09-09