ci基础知识总汇(持续更新)

来源:互联网 发布:mac开机选择用户 编辑:程序博客网 时间:2024/05/01 15:34

1、控制器目录中建立目录无法访问默认控制器的解决办法

routes.php的配置是:

$route['default_controller'] = "welcome";$route['404_override'] = '';
application/controllers目录下新建个目录admin,做为后台用

admin目录下有个welcome.php

通过http://127.0.0.1/index.php/admin访问的页面是404错误。但是通过http://127.0.0.1/index.php/admin/welcome 可以访问。

注明:因为出现这个问题是在win2003+iis6.0上测试的,在“CI中国论坛”没有找到相关资料,但看到了大家不建议CI用在IIS环境下,所以我后来屏蔽了服务器的IIS,并安装了apache。奇怪的是同样无法访问。

经过研究发现,如果想要通过http://127.0.0.1/index.php/admin来访问控制器目录application/controllers/admin/下的默认控制器welcome.php,那么在controllers目录下必须也要有welcome.php控制器。经过测试,apache和iis下都可以通过http://127.0.0.1/index.php/admin来访问admin目录下的默认控制器了。

2、传递中文参数

当我使用jquery的方式获取值,通过URL的方式传参,后台通过$this->uri->segment(n)的方式接收值时是乱码。解决方案是对接收的值经过两次urldecode处理

urldecode(urldecode($this->uri->segment(n)))
这一发现来源于一个项目,源码如下:

$(".edit").bind("click",function(event){//编辑一级分类var id=$('input[name=id]',$(this).parents('form:first')).val();var parentId=$('input[name=parentId]',$(this).parents('form:first')).val();var name=$('input[name=name]',$(this).parents('form:first')).val();var img=$('input[name=img]',$(this).parents('form:first')).val();//alert(name);//name=encodeURIComponent(encodeURIComponent(name));//alert(encodeURIComponent(name));//alert(encodeURIComponent(encodeURIComponent(name)));art.dialog.open(edit_url+'/'+id+'/'+parentId+'/'+name+'/'+img,{id:'edit',title: '修改分类〖'+name+'〗',lock:true,resize:false},false);}).addClass("pointer");
这里可以对name,用encodeURIComponent两次编码,后台通过urldecode两次解码也可以实现相同功能。但是,如果对name进行两次encodeURIComponent,那么在art.dialog.open方法中的:'修改分类〖'+name+'〗' 中的name将会是编码后的值,而不是汉字。于是去掉encodeURIComponent编码后,发现尽然可以。哪道是CI自动对汉字进行了编码?暂不管这些了。

为什么这里我想到的用encodeURIComponent两次编码,这要看另一篇相关文章:ajax/post/gb2312特殊字符出现乱码完美解决方法

2012-12-28更新:

上面说了,去掉encodeURIComponent,在后台通过两次urldecode解码后获取中文正常。但是今天将程序上传到服务器确发现不行。原因在于服务器上是IIS6.0,而不是apache,所以,上面所说的内容在IIS6.0下不适用。当然,可以在js脚本中对包含中文的内容两次encodeURIComponent后,在apahce和IIS6.0都可适用。后台接收不变,即两次urldecode对编码的内容进行解码。

3、当编编辑一篇文章时,form_validation如何重新填充表单?

set_value()可以有默认值

set_value('title', isset($article->title) ? $article->title : '');
问题来源:http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=7568