Yii整合Ckeditor和Ckfinder上传文件

来源:互联网 发布:数据库系统教程王能斌 编辑:程序博客网 时间:2024/04/28 22:32

啥话不说,我要骂两个人!

第一:http://www.cnblogs.com/xiaoyaojian/archive/2012/07/16/2593354.html,这篇文章的作者,

第二:http://hi.baidu.com/layooo/item/1f289ff044de002f743c4cd4 这篇文章的作者,

这两个坑爹的,写文章你要写完整呀,哪能这样坑我们这些天真的孩子呢!!!让我蛋碎一下午!!!不说了,切入正题。

1.准备

首先到http://ckeditor.com/ 下载ckeditor;

然后到http://ckfinder.com/ 下载ckfinder;

最后到http://www.yiiframework.com/extension/ckeditor-integration 下载ckeditor widget
2.安装

将下载到的ckeditor和ckfinder的zip包,解压到yii项目的根目录,并将ckeditor widget解压到yii项目的extension,形成的目录结果如下图所示:


3.配置

首先请参看yii的这个扩展:
http://www.yiiframework.com/extension/ckeditor-integration/(下载wedget的zip文件)

这个扩展整合了ckeditor,但美中不足的是没集成ckfinder,于是对其手工修改,已能正常使用
我的主要路径是这样的

MyApp(项目的目录)
----ckeditor
----ckfinder
----protected
......略

上边的扩展文件就是两个,
protected/extensions/ckeditor/CKEditorWidget.php
protected/extensions/ckeditor/views/CKEditorView.php
下边就是我对这两个文件修改后的样子
1.
CKEditorWidget.php
代码:
class CKEditorWidget extends CInputWidget
{

public$ckEditor;
public$ckFinder;
public$ckBasePath;
public$defaultValue;
public$config;

publicfunction run()
{
if(!isset($this->model)){
thrownew CHttpException(500,'"model" 必需设置!');
}
if(!isset($this->attribute)){
thrownew CHttpException(500,'"attribute" 必须设置!');
}
if(!isset($this->ckFinder)){
$this->ckFinder = Yii::app()->basePath."/../ckfinder/ckfinder.php";
}
if(!isset($this->ckEditor)){
$this->ckEditor = Yii::app()->basePath."/../ckeditor/ckeditor.php";
}
if(!isset($this->ckBasePath)){
$this->ckBasePath = Yii::app()->baseUrl."/ckeditor/";
}
if(!isset($this->defaultValue)){
$this->defaultValue ="";
}

$controller=$this->controller;
$action=$controller->action;
$this->render('CKEditorView',array(
"ckFinder"=>$this->ckFinder,
"ckEditor"=>$this->ckEditor,
"ckBasePath"=>$this->ckBasePath,
"model"=>$this->model,
"attribute"=>$this->attribute,
"defaultValue"=>$this->defaultValue,
"config"=>$this->config,
));
}
}
CKEditorView.php (同在wigdet中的view内)
代码:
require_once($ckEditor);
require_once($ckFinder);

$oCKeditor=new CKeditor(get_class($model).'['.$attribute.']');
$oCKeditor->basePath =$ckBasePath;

if(isset($config) &&is_array($config)){
foreach($config as $key=>$value){
$oCKeditor->config[$key] =$value;
}
}

CKFinder::SetupCKEditor($oCKeditor, Yii::app()->baseUrl .'/ckfinder/');
$oCKeditor->editor(get_class($model).'['.$attribute.']',$defaultValue);

这样,就可以使用最新的ckeditor与ckfinder了,剩下的就是修改ckeditor与ckfinder的配置以适合要求,例如ckfinder上传中文文件时文件名问题的修改等等,网上有许多这方面的文章


5.打开 项目/ckfinder/config.php,配置以下内容

$baseUrl = ‘upload/’;//上传的文件所在的根目录

$baseDir=’F:/php_dev/apache/htdocs/DvoraBlog/upload/’;//根目录所在的绝对地址

我们来看ckfinder里的config.php里的一句注视:
$baseDir : the path to the local directory (in the server) which points to the
above $baseUrl URL. This is the path used by CKFinder to handle the files in
the server. Full write permissions must be granted to this directory.

Examples:
// You may point it to a directory directly:
$baseDir = ‘/home/login/public_html/ckfinder/files/’;
$baseDir = ‘C:/SiteDir/CKFinder/userfiles/’;

// Or you may let CKFinder discover the path, based on $baseUrl.
// WARNING: resolveUrl() *will not work* if $baseUrl does not start with a slash (“/”),
// for example if $baseDir is set to http://example.com/ckfinder/files/
$baseDir = resolveUrl($baseUrl);
大致意思 就是说 如果你baseUrl使用的是相对地址 那么resolveUrl()这个函数就废掉了,你得自己手动设置$baseDir,并给出了win下linux下的配置实例。
4.使用

在需要使用文本编辑器的时候,使用widget方式加入到页面中01 <?php $this->widget('ext.ckeditor.CKEditorWidget',array(
02 "model"=>$model, # 数据模型
03 "attribute"=>'content', # 数据模型中的字段
04 "defaultValue"=>"Test Text", # 默认值 "config" => array(
05 "height"=>"400px",
06 "width"=>"100%",
07 "toolbar"=>"Full",#工具条全部显示,
08 "filebrowserBrowseUrl"=>'/ckfinder/ckfinder.php' #这里很关键,设置这个后,打开上传功能和浏览服务器功能
09 ),
10 #Optional address settings if you did not copy ckeditor on application root
11 #"ckEditor"=>Yii::app()->basePath."/ckeditor/ckeditor.php",
12 # Path to ckeditor.php
13 #"ckBasePath"=>Yii::app()->baseUrl."/ckeditor/",
14 # Realtive Path to the Editor (from Web-Root)
15 ) );
16 ?>


到此,我都是按照我骂的第一位仁兄的方法配置的,打开页面一看,咦,激动了,出来了浏览服务器按钮,可是,但我点下去的时候蛋碎了,尼玛,是空白!!这就是我骂他的原因。

接下来我找啊找啊,终于找到一个貌似靠谱的了,也就是第二位仁兄的补充方法

在protected/extensions/ckeditor/views/CKEditorView.php下加上这一句:1 CKFinder::SetupCKEditor($oCKeditor, Yii::app()->baseUrl .'/ckfinder/');


尼玛,这不加还好,一加果断报错。但我的感觉告诉我这位仁兄的方法靠谱,于是我看了一下错误,原来时自动加载出错。仔细分析一下,以为CKFinder是文件ckfinder下的core下的ckfinder_php5.php里的类,因为这个问价是位于protected文件夹外,所以无法自动加载。于是我在protected/extensions/ckeditor/views/CKEditorView.php里include进来ckfinder/core/ckfinder_php5.php这个问件,Ok,好了!庆贺一下!