在项目中我们经常需要使用富文本编辑器(什么是富文本?), 很多时候我们需要在我们编辑的内容中插入媒体文件, 比如图片,视频等等, 这就要涉及到富文本编辑器的上传功能, 本文主要讲解如何在symfony中使用FCKeditor和如何配置上传文件. 富文本编辑器我是更喜欢tinyMCE的, 但由于tinyMCE对上传的支持并不好, ImageManager等插件都是收费使用的, 所以不得不转向了FCKeditor. 首先我们进入FCKeditor的官方主页www.fckeditor.net下载源码包(http://www.fckeditor.net/download), 当前最新版本是2.6.4. 下载完源之后我们将压缩包解压到项目的web/js目录下, 修改要使用FCKeditory的app的config/settings.yml, 添加代码如下: all: 如果不使用上传功能, 那么现在FCKeditory就可以使用了.
.settings:
rich_text_fck_js_dir: js/fckeditor// app/editor/modules/test/templates/indexSuccess.php 访问http://test/editor_dev.php/test/index 效果如下
<?php use_helper('Form');?>
<?php echo textarea_tag('name', 'default content', array ('rich'=>'fck','tool'=>'Basic','size' => '30x30')) ?>
接下来开始配置上传图片附件, 这个功能是FCKeditor的功能, 所以要在FCKeditor进行配置. 首先编辑web/js/fckeditor/fckconfig.js var _FileBrowserLanguage = 'php' ; // asp | aspx | cfm | lasso | perl | php | py 然后编辑 web/js/fckeditor/editor/filemanager/connectors/php/config.php
var _QuickUploadLanguage = 'php' ; // asp | aspx | cfm | lasso | php$Config['Enabled'] = true; web/js/fckeditor/editor/filemanager/connectors/php/config.php中还可以配置上传的其他相关参数, 比如文件类型等等. 现在上传功能就设置好了.
$Config['UserFilesPath'] = '/uploads/';// app/editor/modules/test/templates/indexSuccess.php 访问页面打开插入图片功能, 画面如图.
<?php use_helper('Form');?>
<?php echo textarea_tag('name', 'default content', array ('rich'=>'fck','tool'=>'Default','size' => '30x30')) ?>
进入上传面板, 画面如图
我们在本地选择图片, 进行上传, 如果使用的是symfony默认的配置, 应该会报错如下.
错误内容, Error creating folder “redirect:/index.php” (Can’t create redirect: directory) 经过一番研究, 最终发现这个错误是由apache的mod_rewrite造成的, 最终发现是web/.htaccess中的这一行导致的 RewriteRule ^([^.]+)$ $1.html [QSA] 由于本人道行还不够, 最终也没有参透.htaccess中的代码都是什么作用, 但经过测试发现将这行去掉程序是可以正常运行的, 不过既然存在就一定有存在的意义, 所以使用了另一个方法来解决这个问题. 我们在web/uploads目录下再建立一个.htaccess, 内容如下 <IfModule mod_rewrite.c> 这样我们可以正常上传图片了.
RewriteEngine Off
</IfModule>
在generator.yml中使用富文本编辑器 这里看一个例子, 出自《symfony权威指南》 generator: 如何在form中使用富文本编辑器? 《How to use FCKeditor》里提供了一个widget, 我没有用过, 但听同事说可能不好用, 所以自己写了一个.
class: sfPropelAdminGenerator
param:
model_class: Comment
theme: default
edit:
fields:
## 不显示表单控件, 只显示文本
id: {type:plain}
## 表单控件不可编辑
author: {disabled=true}
## 富文本编辑器 (object_textarea_tag)
content: {type: textarea_tag, params: rich=true css=user.css tinymce_options=width:330}
## 下拉列表 (object_select_tag)
article_id: {params: include_custom=Choose an article}//lib/widget/sfWidgetFormFCK.class.php
<?
class sfWidgetFormFCK extends sfWidgetFormTextarea
{
public function render($name, $value = null, $attributes = array(), $errors = array())
{
sfContext::getInstance()->getConfiguration()->loadHelpers('Form');
return textarea_tag($name, $value, array_merge($attributes, array('rich' => 'fck', 'tool' => 'Default' )));
}
}//lib/form/ShopForm.class.php 参考: 《Symfony TinyMCE & FCKeditor》 《How to use FCKeditor》 (本文完)
<?php
class ShopForm extends BaseShopForm
{
public function configure()
{
$this->setWidget('sid', new sfWidgetFormFCK());
}
}
2009-02-11
在symfony中使用FCKeditor上传图片附件
7 条评论 »
这篇文章上的评论 RSS feed TrackBack URL

在后台如何使用FCK编辑器呢··?
还有个人觉得你使用的方法没有带定义编辑器大小的参数,我找到一个别人写的方法可能更好用哦···
http://forum.symfony-project.org/index.php/m/74557/?srch=FCK#msg_74557
Comment 由 Assassinann — 2009-03-26 @ 13:52
还有···我有点不明白的地方就是您编写的sfWidgetFormFCK.class.php
文件是放在app目录下还是在frontend目录下呢?
Comment 由 Assassinann — 2009-03-26 @ 14:06
感谢您的关注,第一个问题,编辑器的大小是可以通过参数进行定义的,但是我写本文的目的不是为了去编写一个功能强大的Widget,所以文中的代码只设计基本功能;第二个问题,class.php文件只要放在可以被自动加载的目录下就可以正常工作,也就是项目的lib目录,app的lib目录,module的lib目录或者插件的lib目录也等位置,不过我建议你放在/lib/widget中,以保持结构清晰。
Comment 由 maker — 2009-03-26 @ 14:14
apps/lib or frontend/lib?
Comment 由 Assassinann — 2009-03-26 @ 14:17
项目根目录下的lib/widget
Comment 由 maker — 2009-03-26 @ 14:21
恩···受教受教····谢谢了···
我觉得这里的资料比symfony中文上的要全面啊··
好多都是您自己亲身实践过的吧···请问您用symfony多长时间了啊·?
Comment 由 Assassinann — 2009-03-26 @ 14:28
这里内容都是公司团队成员的工作总结,都是文章原作者亲自实践过的,第一个使用symfony的项目是从08年11月末,这个项目现在处于收尾阶段。
Comment 由 maker — 2009-03-26 @ 14:50