问题描述
在Catalyst应用程序或模板中定义的数据具有正确的编码,并且显示良好,但是从数据库中,所有非Latin1
的数据都转换为?
.我想问题应该在模型类中,是这样的:
Data defined inside Catalyst app or in templates has correct encoding and is diplayed well, but from database everything non-Latin1
is converted to ?
. I suppose problem should be in model class, which is such:
use strict;
use base 'Catalyst::Model::DBIC::Schema';
__PACKAGE__->config(
schema_class => 'vhinnad::Schema::DB',
connect_info => {
dsn => 'dbi:mysql:test',
user => 'user',
password => 'password',
{
AutoCommit => 1,
RaiseError => 1,
mysql_enable_utf8 => 1,
},
'on_connect_do' => [
'SET NAMES utf8',
],
}
);
1;
我在这里看不到任何缺陷,但是一定有问题.我还在测试脚本中使用了我的架构,数据得到了很好的编码,输出是正确的,但是在Catalyst应用程序中,我的编码没有正确.可能是哪里出问题了?
I see no flaws here, but something must be wrong. I used my schema also with test scripts and data was well encoded and output was correct, but inside Catalyst app i did not get encoding right. Where may be the problem?
编辑
为了将来参考,我将解决方案放在这里:我将连接信息混合在一起,将旧样式和新样式混合在一起.
For future reference i put solution here: i mixed in connect info old and new style.
旧样式就像(dsn, username, passw, hashref_options, hashref_other options)
新样式为(dsn => dsn, username => username, etc)
,因此正确使用:
New style is (dsn => dsn, username => username, etc)
, so right is to use:
connect_info => {
dsn => 'dbi:mysql:test',
user => 'user',
password => 'password',
AutoCommit => 1,
RaiseError => 1,
mysql_enable_utf8 => 1,
on_connect_do => [
'SET NAMES utf8',
],
}
推荐答案
在使用Catalyst :: View :: TT和Catalyst :: Model :: DBIC :: Schema的典型Catalyst设置中,UTF- 8工作:
In a typical Catalyst setup with Catalyst::View::TT and Catalyst::Model::DBIC::Schema you'll need several things for UTF-8 to work:
- 在您的Catalyst应用中添加Catalyst :: Plugin :: Unicode :: Encoding
- 将
encoding => 'UTF-8'
添加到您的应用程序配置中 - 将
ENCODING => 'utf-8'
添加到您的TT视图配置中 - 将
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
添加到html的<head>
部分中,以满足不关心Catalyst :: Plugin :: Unicode :: Encoding 设置的 - 如果模板包含非ASCII字符,请确保您的文本编辑器将模板保存为UTF-8
- 根据 DBIx :: Class配置DBIC模型: :Manual :: Cookbook#使用Unicode
- 如果使用Catalyst :: Authentication :: Store :: LDAP,请通过添加
ldap_server_options => { raw => 'dn' }
将LDAP存储配置为返回UTF-8.
Content-Type:text/html; charset=utf-8
http标头的旧IE.- add Catalyst::Plugin::Unicode::Encoding to your Catalyst app
- add
encoding => 'UTF-8'
to your app config - add
ENCODING => 'utf-8'
to your TT view config - add
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
to the<head>
section of your html to satisfy old IEs which don't care about theContent-Type:text/html; charset=utf-8
http header set by Catalyst::Plugin::Unicode::Encoding - make sure your text editor saves your templates in UTF-8 if they include non ASCII characters
- configure your DBIC model according to DBIx::Class::Manual::Cookbook#Using Unicode
- if you use Catalyst::Authentication::Store::LDAP configure your LDAP stores to return UTF-8 by adding
ldap_server_options => { raw => 'dn' }
根据 Catalyst :: Model :: DBIC :: Schema# connect_info :
还支持使用带有DBI的hashref的旧arrayref样式,然后支持DBIx :: Class选项.
但是您已经在使用'new'样式,因此您不应该嵌套dbi属性:
But you are already using the 'new' style so you shouldn't nest the dbi attributes:
connect_info => {
dsn => 'dbi:mysql:test',
user => 'user',
password => 'password',
AutoCommit => 1,
RaiseError => 1,
mysql_enable_utf8 => 1,
on_connect_do => [
'SET NAMES utf8',
],
}
这篇关于如何在Catalyst应用程序内的架构中正确使用UTF-8编码的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!