Yii框架集成CKEditor及去除多余标签的方法
本示例详解了如何在Yii框架中正确引用外部插件,以及解决CKEditor编辑器输出内容时自动产生多余body和P标签的问题。相关内容可供开发者参考实践。
探讨在Yii环境中引入插件的具体步骤。
需注意区分插件(Plugin)与扩展(Extension)的概念差异。
1,源码放在project/ckeditor/* 2,在代码create,update中引用
具体Php代码实现如下。
<?php
include_once "/ckeditor/ckeditor.php";
// Create a class instance.
$CKEditor = new CKEditor();
// Path to the CKEditor directory.
$CKEditor->basePath = Yii::app()->baseUrl.'/ckeditor/';
// Replace a textarea element with an id (or name) of "textarea_id".
$CKEditor->replace("News_content");
?>
引入插件的第二种方法:
首先,以使用Zend Framework为例,将其解压至protected/vendors目录,此时文件路径为protected/vendors/Zend/Search/Lucene.php。
其次,在Controller文件的头部插入以下代码。
Yii::import('application.vendors.*');
require once('Zend/Search/Lucene.php');
上述代码旨在包含Lucene.php类文件。由于采用相对路径,需调整PHP的文件加载路径,且必须确保Yii::import调用位于require_once之前。
最后,配置完成后即可在Controller中直接使用该组件。例如:
$lucene=new Zend Search Lucene($pathOfIndex);
$hits=$lucene->find(strtolower($keyword));
项目中集成了CKEditor,但发现编辑框下方会多出body和P两个标签。经排查,这是组件自身特性而非手动添加所致。
在使用JS初始化申请处新增相关配置。
CKEDITOR.config.removePlugins='elementspath';
通过该设置即可成功去除多余的标签。
希望上述内容对基于Yii框架进行PHP程序设计的开发者有所帮助。