PHPEye开源社区 » Zend Framework 使用讨论 » ZF与FCKeditor集成完全攻略(包括上传、浏览服务器图像)
《Programming PHP》第二版上市
2007-7-29 23:25 Haohappy
ZF与FCKeditor集成完全攻略(包括上传、浏览服务器图像)

FCKedtior是目前最流行和最强大的HTML在线编辑器之一,非常好用。本文介绍如何集成Zend framework和FCKeditor。

首先基本配置的修改,网上很多文章都有写,在此不再多说,最权威的资料当然是FCKeditor官网的:
[url]http://wiki.fckeditor.net/Developer%27s_Guide/Integration/PHP[/url]

最主要是修改fckconfig.js文件。

下面说说和ZF的集成,以及一些需要注意的地方:

[color=Red][b]1。FCKeditor的安装[/b][/color]
FCKeditor需要放在public的目录,即通过网站可以访问到的目录下。比如wwwroot/js/FCKeditor。

新版的FCKeditor需要加载一些配置XML文件,所以在你的Apache配置中,XML文件不能转向到ZF的bootstrap。
另外,下面的一些功能需要用到一些PHP文件,而在ZF项目中通常禁用对PHP文件的直接访问。

这些你都可以通过修改Apache服务器的URL重写规则来搞定:
RewriteCond %{REQUEST_URI} !^.*(\.html|\.xml|\.css|\.js|\.gif|\.png|\.jpg|\.jpeg)$|.*(FCKeditor).*

这样,XML文件不转向至index.php,而访问地址中包含FCKeditor字符的php文件也能直接访问。


[color=Red][b]2。集成[/b][/color]

在你的模板文件中(即ZF的View部分,例如views/article/articleForm.php),需要添加表单的地方加入以下代码:
[php]<?php
        $oFCKeditor = new FCKeditor('ArticleBody') ;
        $oFCKeditor->BasePath = "/js/FCKeditor/";
        $oFCKeditor->Width  = '600';
        $oFCKeditor->Height = '500';
        if($this->article->ArticleBody!=''){
                $oFCKeditor->Value = $this->article->ArticleBody;
        }else{
                $oFCKeditor->Value = '';
        }
        $oFCKeditor->Create();
        ?>
[/php]

这个表单可以同时用于添加和编辑文章,当文章内容不为空(即当前操作为编辑时),显示文章内容,否则为新增文章,表单内容为空。

提交本页面后,你可以使用$_POST['ArticleBody']来获取表单中的文字内容。


[color=Red][b]2。浏览服务器图像[/b][/color]
在写文章的时候,希望可以直接浏览服务器上的图像,添加到文章里,就需要这个功能。
FCKeditor中本功能的实现在FCKeditor\editor\filemanager\browser\default\connectors\php目录的几个文件。

我们只需要修改
[b]FCKeditor\editor\filemanager\browser\default\connectors\php\config.php[/b]
[php]
$Config['Enabled'] = true ; //一定要设定成true,本功能才启用
$Config['UserFilesPath'] = '/UserFiles/' ;  //图像文件所在的目录,你可以根据自己的需要修改
[/php]

设定好后你可以通过ftp上传图像文件到/UserFiles/image目录下,测试一下是否可以浏览。

[b]注意:[/b]默认情况下,FCKeditor的图像文件要放在UserFiles下的image目录里,而不能直接放在UserFiles目录里。

[color=Red][b]3。图像上传[/b][/color]

如果你想在写文章的时候,直接上传图像到服务器,然后插入到文章中,可以用这个功能。
FCKeditor中本功能的实现在FCKeditor\editor\filemanager\upload\php目录的几个文件。

我们也只需要修改该目录下的config.php

[php]
$Config['Enabled'] = true ;
$Config['UserFilesPath'] = '/UserFiles/' ; //上传目录的路径,通常和上面的浏览部分的路径设成一样的
$Config['UseFileType'] = true ;  //不同上传文件类型是否分目录放置,图像文件会自动被上传到/UserFiles/image目录下,Flash则在/UserFiles/flash目录下
[/php]

[b]注意:[/b]UserFiles目录和其下的image目录要可写权限。

[[i] 本帖最后由 Haohappy 于 2007-7-30 08:20 编辑 [/i]]

2007-7-31 21:46 kylingood
经典。收下了~~~~~~~~~:lol :lol

2007-8-3 22:07 kuco
支持

2007-8-12 05:15 raylzw
支持.先收了,

2007-8-13 23:04 scauren
VIEW用到smarty的怎么做呢?

2007-11-7 11:48 leinchu
fckeditor_php5.php另存为:libs/Custom/Fckeditor.php,类名改为:Custom_FCKeditor.
controller:
public function fckAction()
        {
                Zend_Loader::loadClass('Custom_Fckeditor');
                $oFCKeditor = new Custom_Fckeditor('ArticleBody') ;
                $oFCKeditor->BasePath = "/scripts/FCKeditor/";
                $oFCKeditor->Width  = '600';
                $oFCKeditor->Height = '500';
                /*if($this->article->ArticleBody!=''){
                        $oFCKeditor->Value = $this->article->ArticleBody;
                }else{
                }*/
                $oFCKeditor->Value = '';
                $this->view->oFCKeditor=$oFCKeditor;
        }

smarty view:
<{include file="header.html"}>
<{ $oFCKeditor->create() }>
<{include file="footer.html"}>

[[i] 本帖最后由 leinchu 于 2007-11-7 11:50 编辑 [/i]]

2007-12-15 17:10 Dereky
安装楼上的方法安装不行啊,报告:
Fatal error: Call to undefined function FCKeditor_IsCompatibleBrowser() in
。。。:'(

2007-12-15 17:16 Dereky
需要修改这个
fckeditor_php5.php
在最后增加
function FCKeditor_IsCompatibleBrowser()
        {
                global $HTTP_USER_AGENT ;

                if ( !isset( $_SERVER ) ) {
                        global $HTTP_SERVER_VARS ;
                        $_SERVER = $HTTP_SERVER_VARS ;
                }

                if ( isset( $HTTP_USER_AGENT ) )
                $sAgent = $HTTP_USER_AGENT ;
                else
                $sAgent = $_SERVER['HTTP_USER_AGENT'] ;

                if ( strpos($sAgent, 'MSIE') !== false && strpos($sAgent, 'mac') === false && strpos($sAgent, 'Opera') === false )
                {
                        $iVersion = (float)substr($sAgent, strpos($sAgent, 'MSIE') + 5, 3) ;
                        return ($iVersion >= 5.5) ;
                }
                else if ( strpos($sAgent, 'Gecko/') !== false )
                {
                        $iVersion = (int)substr($sAgent, strpos($sAgent, 'Gecko/') + 6, 8) ;
                        return ($iVersion >= 20030210) ;
                }
                else if ( strpos($sAgent, 'Opera/') !== false )
                {
                        $fVersion = (float)substr($sAgent, strpos($sAgent, 'Opera/') + 6, 4) ;
                        return ($fVersion >= 9.5) ;
                }
                else if ( preg_match( "|AppleWebKit/(\d+)|i", $sAgent, $matches ) )
                {
                        $iVersion = $matches[1] ;
                        return ( $matches[1] >= 522 ) ;
                }
                else
                return false ;
        }

然后 105行改成
return $this->FCKeditor_IsCompatibleBrowser() ;

2007-12-20 22:24 edwin
也参加讨论一下:
既然提到与zf 完全整合,我想还是要考虑一下几个问题,不知道楼主的意见如何:
1 fckAction将多次出现,如何抽象他
2 zf的public既然不建议有其他的php文件,我们还是要将fck的php file移植,不知道楼主是怎么想的?

2008-1-5 19:43 古树
快速上传、浏览服务器、创建目录均不支持中文文件名。。。

寻求最完美的解决办法??

2008-1-18 13:07 trooman
我认为主要代码放在cotroller里会更好,这样的话就不用在每个视图文件里重复做同样的工作了,看看我的框架中是怎么用的吧,仅供参考:

[code]
public function setFCKHTML($fckname, $value = null)
        {       
                $path1 = substr(dirname($_SERVER['SCRIPT_NAME']), 1);
                $path2 = str_replace('\\', '/', dirname(APP_DIR));
                $pieces = explode('/', $path1);
               
                $ar = array();
                foreach ($pieces as $val)
                {
                        if (preg_match("/\/$val/is", $path2))
                        {
                                $ar[] = $val;
                        }
                }
                $path = join('/', $ar);
               
                if (!empty($path))
                {
                        $path = '/' . $path;
                }
                $oFCKeditor = new FCKeditor($fckname);
                $base_path = $path . '/frame/fckeditor/';
                $oFCKeditor->BasePath = $base_path;
                $oFCKeditor->Height     = '360';
                $oFCKeditor->Width      = '98%';
                $oFCKeditor->ToolbarSet = 'Terse';
                $oFCKeditor->Value                = $value;
                return $oFCKeditor->CreateHTML();
        }

public function addEditAction()
        {
                .......
                require(dirname(APP_DIR) . "/frame/fckeditor/FCKeditor.php");
                $this->view->contentIframe = $this->setFCKHTML("news[content]", $this->news->content);
        }

[/code]

在view中,直接{{$this.contentIframe}}, 很简单!

补充一下,我的代码就算主程序(index.php)不放在根目录下,如二级目录/admin/index.php,一样可以运行!

[[i] 本帖最后由 trooman 于 2008-1-18 13:19 编辑 [/i]]

2008-1-18 13:31 haohappy
to 楼上,这样也不好,我觉得放在一个View_helper里更自然一些。 比如说Zend_View中提供了很多用于生成表单元素的helper,同样的道理,你自己写一个生成Fckeitor表单的就行了。

2008-1-18 15:24 trooman
呵呵,说的不错,我的目的是减少重复工作,放在View_helper里确实更好,只是我的框架暂时还没有View_helper。

2008-2-2 20:17 superwen
[quote]原帖由 [i]haohappy[/i] 于 2008-1-18 13:31 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=1233&ptid=117][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]
to 楼上,这样也不好,我觉得放在一个View_helper里更自然一些。 比如说Zend_View中提供了很多用于生成表单元素的helper,同样的道理,你自己写一个生成Fckeitor表单的就行了。 ... [/quote]


支持这样的做法!很好。

2008-4-14 10:22 Freebear
:) :) :) :victory: :victory: :victory:

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.