Yii2 integration Baidu editor umeditor and umeditor image Upload solution, yii2umeditor_PHP tutorial

Source: Internet
Author: User
Yii2 integrates Baidu editor umeditor and umeditor image Upload solution, yii2umeditor. Yii2 integrates Baidu editor umeditor and umeditor Image uploading solution. yii2umeditor will talk about how Yii2 Framework integrates Baidu editor umeditor. Umedi yii2 integration Baidu editor umeditor and umeditor image Upload solution, yii2umeditor

Next, let's talk about how the Yii2 Framework integrates Baidu editor umeditor.

What is umeditor? I have only heard of ueditor. Are you a pirated umeditor? Umeditor, to put it bluntly, is the mini version of ueditor. according to Baidu's official statement, it is actually the "short soft and small" in the editor, but the function is optimized. Ke, let's get back to the question.

First, let's download a mini version of ueditor umeditor on the official website. Note that it's um editor.

Download the package, decompress it, and put it in the/css directory under the Project root directory and name it umeditor. you can refer to it later.

Step 2: expand the backend \ assets \ Appset class first. why do we need to extend this kind of stuff and integrate it with our umeditor. The purpose of extending this class file here is to facilitate the introduction of css js file drops in the file.

It's easy to add the following two methods to the Appset method:

// Define the JS method to be loaded as needed. pay attention to the loading order in the final public static function addScript ($ view, $ jsfile) {$ view-> registerJsFile ($ jsfile, [AppAsset :: className (), 'desc' => 'backend \ assets \ appasset']);} // defines the on-demand loading css method, note that the loading sequence is in the final public static function addCss ($ view, $ cssfile) {$ view-> registerCssFile ($ cssfile, [AppAsset: className (), 'desc' => 'backend \ assets \ appasset']);}

Next, follow the configuration below.

Here, we assume there is an article table, and a content field needs to be displayed as Baidu editor.

According to the form model of yii2, we modify the content field in the article \ _ form. php file.

<?= $form->field($model, 'content')->textarea(['style' => 'width:760px;height:500px;']) ?> 

This file introduces the Appset class and introduces the relevant css js file as follows:

use backend\assets\AppAsset;AppAsset::register($this);AppAsset::addCss($this,'/css/umeditor/themes/default/css/umeditor.css');AppAsset::addScript($this,'/css/umeditor/umeditor.config.js');AppAsset::addScript($this,'/css/umeditor/umeditor.min.js');AppAsset::addScript($this,'/css/umeditor/lang/zh-cn/zh-cn.js');

Then, you only need to register the following js code at the bottom of the current page.

<?php $this->beginBlock('js-block') ?>$(function () {var um = UM.getEditor('article-content', {});});<?php $this->endBlock() ?><?php $this->registerJs($this->blocks['js-block'], \yii\web\View::POS_END); ?> 

This is the target object to be bound, that is, content. Article-content is the id of the current object.

OK. The Baidu editor is basically integrated here. Now, add an article and try again. remember to update it to see if there is any content in the editor.

The following describes how yii2 solves the problem of umeditor Image uploading in Baidu editor.

The yii2 Framework integrates the Baidu editor. because the file is uploaded using the UploadedFile provided by yii2, the umeditor Upload will inevitably fail. to solve the problem, only two steps are required, let's take a look at the specific implementation

First, we need to configure umeditor. here we only need to change the imageUrl configuration item. we can modify it to point to/tools/um-upload.

The next step is to implement the/tools/um-upload method,

According to the ueditor implementation, we only need to return the successful message after the upload is successful.

Use backend \ models \ Upload; use yii \ web \ UploadedFile;/*** Baidu umeditor Upload */public function actionUmUpload () {$ model = new Upload (); if (Yii ::$ app-> request-> isPost) {$ model-> file = UploadedFile: getInstance ($ model, 'file '); $ dir = 'File save directory'; if (! Is_dir ($ dir) mkdir ($ dir); if ($ model-> validate () {$ fileName = $ model-> file-> baseName. '. '. $ model-> file-> extension; $ dir = $ dir. '/'. $ fileName; $ model-> file-> saveAs ($ dir); $ info = ["originalName" => $ model-> file-> baseName, "name" => $ model-> file-> baseName, "url" => $ dir, "size" => $ model-> file-> size, "type" => $ model-> file-> type, "state" => 'success',]; exit (json_encode ($ info ));}}}

Note: in the $ info returned above, the state can only be SUCCESS, case sensitive.

Let's talk about how the Yii2 Framework integrates the Baidu editor umeditor. Umedi...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.