在filters.yml文件中加入:

xmlhttpUtf8DecodeFilter:
class: xmlhttpUtf8DecodeFilter


添加一个xmlhttpUtf8DecodeFilter.class.php

  1. <?php
  2. /**
  3. * This filter acts on AJAX requests and decodes the
  4. * request parameter values using the php utf8_decode()
  5. * function.
  6. *
  7. * @author jmunz <jochen.munz@virn.de>
  8. */
  9. class xmlhttpUtf8DecodeFilter extends sfFilter {
  10.  
  11.     /**
  12.      * Run the filter
  13.      */
  14.     public function execute ($filterChain) {
  15.         $request = $this->getContext()->getRequest();
  16.         // Only act if this is an ajax request
  17.         if ( $request->isXmlHttpRequest() && $this->isFirstCall() ){
  18.             $params = $request->getParameterHolder()->getAll();
  19.             $this->decodeParameters($params);
  20.         }
  21.         // execute next filter
  22.         $filterChain->execute();
  23.     }
  24.  
  25.     /**
  26.      * Decode parameters
  27.      */
  28.     private function decodeParameters(&$params){
  29.         foreach (array_keys($params) as $key) {
  30.             if ( is_array($params[$key]) ) {
  31.                 $this->decodeParameters($params[$key]);
  32.             } else {
  33.                 $params[$key] = utf8_decode($params[$key]);
  34.             }
  35.         }
  36.     }
  37.  
  38. }

  5 Responses to “symfony中使用ajax采用utf-8编码”

  1. 你好,看到你写了这么多关于symfony的文章,想跟你交流一下,symfony现在不支持utf-8格式的yml文件,这个开发中文网站带来一些困难,比如:在view.yml文件中加入一些中文,如网页title,keyword等meta信息,如果html页面用utf-8的话会产生乱码,只有使用gb2312才正确显示,但如果html页面使用gb2312的话,会有一些麻烦,比如:jcarlendor和tiny_mce就不能很好的显示中文,还有一些非中文操作系统上不能浏览等。请问你有没有好的方法,可以解决utf-8显示乱码的问题?我曾设想过把所有的symfony的程序都改成utf-8的格式,然后再把所有的controler、view和yml改成utf-8的格式,但不知此方法是否可行。如能交流,请发邮件给我,谢谢。

  2. 可以支持UTF8啊,只要保持时存为UTF8格式的就行。

  3. 我试过把view.yml改成UTF-8格式,但是不行。相反,有些yml文件改成utf-8格式后,页面反而不能正常显示,不知道是不是跟操作系统有关。不过对于view.yml中使用中文乱码的问题我已经找到了解决方法:
    解决view.yml使用UTF-8中文乱码的问题,解决方法如下:
    找到symfony/config/sfViewConfigHandler.class.php中的一段代码:
    htmlentities($content, ENT_QUOTES, sfConfig::get(’sf_charset’)
    把它更改为:
    iconv(”gb2312″, sfConfig::get(’sf_charset’), $content)
    ’sf_charset’ 是 settings.yml中charset的部分,这里设置为utf-8。
    原理如下:
    sfViewConfigHandle解析view.yml中的参数,并在cache/myapp/dev/config和cache/myapp/prod/config下生成modules_xxx_config_view.yml.php文件,xxx代表module的名称。原代码只将$content变量中的字符转换成了utf-8的字符,但生成的modules_xxx_config_view.yml.php文件却是ANSI格式的。所以输出会出现乱码。
    注意:更改以上代码后,view.yml文件用gb2312格式。如果需用到其他格式,可以把iconv的函数中的gb2312改为相应格式。
    此方法,只解决了view.yml中的UTF-8的中文字符问题。至于其他的yml文件还暂时没有找到解决方法。但是,除了validator中需要用到中文字符提示错误以外,其他的基本上不需要有中文。而validator的中文问题可以使用i18n方法或者别的方法解决。

  4. 把有用到中文的文件都用UTF8格式保存,并且页面的charset=utf-8也设置一下,就没什么问题了,www.zzxxw.net就是这样处理的。

  5. 找到原因了,把view.yml改成utf-8就行了。不知为什么,我用windows xp的记事本把view.yml另存为utf-8格式后会有问题。用editplus修改后再保存就行了。谢谢!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   
© 2011 刘敏的Blog Suffusion theme by Sayontan Sinha