Home  >  Article  >  Backend Development  >  How to use the Thinkphp editor extension class kindeditor_PHP tutorial

How to use the Thinkphp editor extension class kindeditor_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:271035browse

How to use the Thinkphp editor extension class kindeditor


1. Preparation before use.
Please make sure you have created a Thinkphp website project before using it.
1. Keditor.class.php and JSON.class.php are editor extension class files. Copy them to the ThinkPHPLibORGNet folder of your website project.
2. The editor folder is the core package of kindeditor. Copy it to the Public folder of your project (the Public at the same level as the entry file), and create an Upload folder under Public to store images uploaded using the editor.
3. KeditorAction.class.php is the editor’s image upload function and remote image browsing function. Copy it to the libAction folder of your project.

2. Object call
Call object in controller method:

import("ORG.Net.Keditor");
$ke=new Keditor();
$ke->id="content";//指定textarea的id
$keshow=$ke->show();//生成js代码
$this->assign("keshow",$keshow);
$this->display();



Display the editor in the template file corresponding to the method:

<html>
<head>{$keshow}</head><!--输出js,建议放在head-->
<body>

</body>
</html>


The above is the simplest calling method, and the extended class also has many properties and methods. To achieve more perfect functions, read on.

3. Object properties
I divide the attributes into two types, one is the kindeditor's own attributes, and the other is the extended new attributes. If you have used kindeditor before, you should know that kindeditor itself has 30 attributes such as id, items, width, height, afterCreate, etc. These attributes can now be defined directly using PHP, such as defining id: $ke->id="content" and defining width: $ke->width="700px"; Let me first talk about the new attributes of the object.
New properties of objects:
1. jspath: Define the core js file of kindeditor. The default value is /Public/editor/kindeditor.js. If your editor folder is not placed under Publib, you need to specify this attribute, such as $ke->jspath="/kind/ kindeditor.js”;
2. Form: Specify the id of the submitted form (from), the default is form1. This attribute is used in combination with the ctrl+enter submission function. For example, if your editor is placed in the form id "formid", you need to implement ctrl +enter submits the formid form, you need to define $ke->form="formid". The ctrl+enter submission function also needs to set other attributes, which will be explained later.
3. imgid: Specifies the hidden domain id where the image address is stored. The default is img. Every time the editor uploads an image, the image address will be stored in this hidden field. When adding data to the database, you can also save the data in this hidden field to the database field. When deleting data, first read the image address stored in the database and delete it. The deletion process only requires calling the delimg method of the object. This method will be explained in detail later. In this way, the purpose of deleting content and pictures is achieved.
Comes with attributes:
1. items: Configure the toolbar of the editor, defined as
$ke->items=”['source', '|', 'fullscreen', 'undo', 'redo', 'print', 'cut', 'copy', 'paste']";
I added the little keyword to quickly define a streamlined model editor, using the method $ke->items="little";
2. afterCreate: Set the callback function to be executed after the editor is created. The definition method is such as
$ke->afterCreate=”function(id){
alert(‘successfully created’+id)
}";
I added the ctrlenter keyword to quickly define ctrl+enter to submit the form. How to use:
$ke->afterCreate=”ctrlenter”;
At the same time, you need to define $ke->form="formname", and replace formname with the form id where your editor is located.
3. resizeMode: 2 or 1 or 0. When 2, you can drag to change the width and height. When 1, you can only change the height. When 0, you cannot drag. The definition method is as follows: $ke->resizeMode=1; Note that numeric attribute values ​​should not be placed in quotation marks, such as $ke->resizeMode="1"; such a definition is wrong.
4. allowFileManager: true or false. When true, the function of browsing server images is displayed (click the upload image button to see this function). The definition method is as follows:
$ke-> allowFileManager=ture;
Note that when the attribute value is a Boolean value, do not put it in quotes.
5. imageUploadJson specifies the server-side program for uploading images. The default value is /index.php/Keditor/upload
6. fileManagerJson: specifies the server-side program for browsing remote images,
The default value is /index.php/Keditor/filemanager
Note: The KeditorAction.class.php you copied before is used for uploading images and browsing remote images. The upload method in the file defines the process of uploading images, and the filemanager method in the file defines the process of browsing images. You can add code to determine permissions to these two methods so that only administrators can upload pictures or browse pictures. You can also define your own upload processing process and image browsing process instead of using the default KeditorAction.class.php. In this case, you need to redefine the imageUploadJson attribute value and fileManagerJson attribute value. The custom processing process will be explained in detail later.
There are other built-in attributes, so I won’t list them all. You can view the official documentation of kindeditor
http://www.kindsoft.net/doc.php?cmd=config
Note that attribute values ​​of numeric type or Boolean type should not be placed in quotation marks. Other attribute values ​​are placed in quotation marks, and the format of the attribute value is the same as the format of kindeditor itself.



四 对象的方法。
1,upload,上传图片。此方法在编辑器上传图片处理过程中使用,使用方法:

import("ORG.Net.Keditor");
Keditor::upload(&#39;./Public/Upload/&#39;,&#39;/Public/Upload/&#39;,array(&#39;gif&#39;,&#39;jpg&#39;,&#39;jpeg&#39;,&#39;png&#39;,&#39;bmp&#39;),1000000);


upload方法有三个参数,依次是,“上传图片目录”,“图片显示地址”,“允许上传图片格式”,“允许的图片大小(单位kb)”
上传图片目录:默认值“./Public/Upload/”(注意Public前面有个“点”符号,是使用的相对地址,不可使用绝对地址),上传图片目录地址是相当于处理文件的。Thinkphp所有的代码都是通过入口文件运行的,所以这个地址其实是相对于入口文件的。
显示图片地址:默认值“/Public/Upload”(一般是绝对地址),假设我们上传了一张图片,服务器端生成的图片名为 12345.gif。上传会显示的图片地址则为/Public/Upload/12345.gif ,因为我们使用的是绝对地址,所以编辑器发布的内容不管在网站的哪儿,图片都能正常显示。
允许上传的图片格式:定义一个数组,默认值为array('gif','jpg','jpeg','png','bmp')
允许的图片大小:默认值为,1000000 ,单位是bk。
2,filemanager,浏览服务器的图片。此方法在浏览图片处理过程中使用。使用方法:

import("ORG.Net.Keditor");
Keditor::filemanager("./Public/Upload/","/Public/Upload/",array(&#39;gif&#39;,&#39;jpg&#39;,&#39;jpeg&#39;, &#39;png&#39;, &#39;bmp&#39;));


参数依次是:“浏览图片目录”,“图片显示地址”,“允许浏览的图片格式”,和upload方法一样,浏览图片目录是相对地址,图片显示地址是觉得地址。

3,delimg:删除通过编辑器上传的图片。此方法一般在你删除数据库数据时使用。

import("ORG.Net.Keditor");
Keditor::delimg($imgfield);
//$imgfield 一般是你数据库存放图片地址的字段。



4,show:返回生成的js代码。此方法一般在显示编辑器的控制器中使用。
show方法可以使用一个参数定义kindeditor自带属性。如:

import("ORG.Net.Keditor");
$ke=new Keditor();
$ke->show(“{
id : ”content”,
width: ‘700px’;
height : ”300px”;
imgid : ”img”
}”);


不建议用show传参方式定义kindeditor属性。show传参方式不能定义jspath和form两个新站属性,也不能使用little和ctrlenter关键词。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/847864.htmlTechArticleThinkphp编辑器扩展类kindeditor使用方法 一, 使用前的准备。 使用前请确认你已经建立好了一个Thinkphp网站项目。 1,Keditor.class.php和JSON.clas...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn