Zend Framework 结合Fckeditor编辑器

我们要做的是把Fckeditor写成一个Zend Framework 插件形式..这样就可以让整个站点调用到FCK... 这样无论是前台和后台都行..

1、下载fckeditor 编辑器。我用的是2.6.5版本的

2、php结合zf环境是搭建完好且能正常运行。

总共步骤有5步。

第一步:新建Fckeditor.php类,即是以Fckeditor下的fckeditor_php5.php为基础的。

第二步:把fckeditor与zf整合

第三步:在zf控制层(controller)调用fckeditor插件

第四步:在视图中显示Fckeditor编辑器

第五步:图片上传调试

第一步:新建Fckeditor.php类

网站开发目录图:

 

最重要的一点就出来了..我把Fckeditor里面的fckeditor_php5.php这个文件写成如下插件形式: 名称就是./Library/Common/Plugin/Fckeditor.php    大致代码如下:

<?php
/** * Zend_Controller_Plugin_Abstract */
require_once 'Zend/Controller/Plugin/Abstract.php';
class Common_Fckeditor extends Zend_Controller_Plugin_Abstract              //要继承这个类
{
public $InstanceName ;             

public $BasePath ;

public $Width ;

public $Height ;

public $ToolbarSet ;

public $Value ;

public $Config ;


public function __construct( $instanceName )
   {
   $this->InstanceName = $instanceName ;
   $this->BasePath   = '/fckeditor/' ;            
   $this->Width   = '100%' ;
   $this->Height   = '200' ;
   $this->ToolbarSet = 'Default' ;              
   $this->Value   = '' ;

   $this->Config   = array() ;
}


public function Create()
{
   echo $this->CreateHtml() ;
}
        //.......


//后面的代码是和FCK里fckeditor_php5.php文件一样的.


//在后面加上 Fckeditor.php 里面这段代码.(一定要加否则会报错.)
public 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 ;
        }
}
?>

第二步:将Fckeditor文件整合到您的ZF中

1.将Fck的源代码放到/public/scripts/下面,如上图/public/script/fckeditor (目录的位置可以根据需要设定)
2.把/Scripts/目录下关闭重写,建立.haccess文件内容:RewriteEngine off,(这个很重要,不关闭重写会导至调不到FCK文件,发生Zend_Controller_Dispatcher_Exception异常)

如图:

第三步:在控制层文件中对FCK调用

<?php

require_once 'Zend/Controller/Action.php';
require_once 'Common/Plugin/Fckeditor.php';  

class IndexController extends Zend_Controller_Action {

   /*添加--信息   显示*/    

public function addAction(){
    $this->view->title = '用户信息添加';
      $fck = new Fckeditor('name');   作为编辑器中的textarea ,隐藏域的名称

     //basepath

    $fck ->BasePath = '/scripts/fckeditor/';                //相对于根目录的地址
      // skins
    $fck ->Config['SkinPath'] = '/scripts/fckeditor/editor/skins/silver/';
   
    $fck ->Width = '100%';
   
    $fck ->Height = '600';
   
    $fck ->Value = '请在这里编写...';   //传入的值应该是 对应各个不同的场景,而显示不同的 用户友好语言
     

    $this->ToolbarSet = 'Default';    //Toolbar 工具条 default & basic 两种;自己定制toolbar?

$this->view->fck = $fck;   //显示到模板。
    }

}

第四步:显示层显示 add.phtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $this->title ?></title>

</head>

<body>

<div style="width:100px; float:left;">请输入内容</div>   <!-- text -->
    <div align="center" style="width:600px;float:left;"><?php echo $this->fck->Create() ?></div>

</body>

</html>

第五步:Fckedotor 上传图片调试

以上步骤都完成后,如不使用图片上传则编辑器可正常使用了;否则,可能会出现这样的错误;Error creating folder “redirect:/index.php/image” (Can’t create redirect: directory)    错误:不能创建文件夹redirect:/index.php/image (不能重定向到这个目录)     这个问题在网上搜了半天,才知道了还是和以上问题一样由于ZF的mod_write url重写造成的。但是我却都没找到那个image的目录,都不知把.htassecc文件放哪里去!?于是又困惑了(汗,还是因为对fckeditor不了解的结果)。。。在这样的一个文件下FCKeditor/editor/filemanager/browser/default/connectors/php/config.php

$Config['UserFilesPath'] = '/UserFiles/' ;     //表示上传服务器上图像的存放文件夹   

老以为这个 '/UserFiles/' 文件已经创建好了,其实却没有;且还是报那个错。于是又在网上查啊,查啊,终于,被我看到了!⊙﹏⊙b汗     这个文件夹是要自己创建的!默认是位于根目录的。 就创建一个/uploads/文件夹,同时这个 $config['UserFilePath'] 也要改。当然不要忘了加上.htaccess文件。

如图:

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

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

设定好后你可以通过ftp上传图像文件到/UserFiles/image目录下,测试一下是否可以浏览。
注意:默认情况下,FCKeditor的图像文件要放在UserFiles下的image目录里,而不能直接放在UserFiles目录里。

 

2.图像上传

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

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

 

以上文章参考资料www.programer.cc/html/PHP/phpjiaocheng/2009/0731/1414.html   而作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值