codeigniter 学习1

来源:互联网 发布:单片机的内部资源 编辑:程序博客网 时间:2024/06/07 20:37
codeigniter 学习1


1 ci中的library和helper,经验要调用的,传统的做法是如下:、
   $this->load->library('pagination');
   $this->load->helper('form');

  但经常这样十分麻烦,可以在system\application\config下的autoload.php中,
设置如下:
   $autoload['libraries'] = array('pagination', 'database');

2 不是必须的,但作为一个好的习惯,可以将system目录挪到www目录下,将application目录挪到与index.php同级的

目录下。好处有两点,第一可以防止CI的核心文件被恶意攻击。第二,将application和system分离的话,即使我们装

了很多网站,也只需要一个system目录,将来更新CI核心文件时,只需要更新一次就能更新全部站点了。更改目录后别

忘了记修改index.php文件,找到

$system_folder = “system”;

更改为

$system_folder = “../CI_system”; (假定你的system文件夹叫CI_system)


3 改apache mod rewrite
   第一步是开启Apache的Mod rewrite模块,打开http.conf文件,找到LoadModule rewrite_module

modules/mod_rewrite.so这句话,将前面的注释去掉。
然后在目录下
<Directory "e:/myphp/">

DirectoryIndex index.html index.php
  AllowOverride All
    Order allow,deny
    Allow from all
</Directory>


在config.php中
  config['base_url']= "http://localhost:8082/myphp/ci";
$config['index_page'] ="";
注意可以用dw等工具,在保存文件时,保存.htaccess如下:
<IfModule mod_rewrite.c>  
<Files ~ "^.(htaccess|htpasswd)$">  
deny from all  
</Files>  
 
Options -Indexes  
Options +FollowSymLinks  
 
#允许解析文件中的SSI指令  
Options +Includes  
 
#定义404,500错误页面  
ErrorDocument 404 /404.htm  
ErrorDocument 500 /404.htm  
 
#定义目录索引页面  
DirectoryIndex index.php  
order deny,allow  
 
RewriteEngine on  
 
#设置根目录  
RewriteBase /www/ci_170/  
 
#去掉链接地址中index.php字符串  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond $1 !^(index.php|images|robots\.txt)  
 
RewriteRule ^(.*)$ index.php/$1 [L]  
 
</IfModule> 

4 CI中的URL辅助函数,比如:
  echo mailto('me@example.com', 'Click Here to Email Me');
就等于
  <a href="mailto:me@example.com">click here to email me</a>

如果防止抓取EMAIL的话,可以用safe_mailto去代替mailto就可以了。

5 页面模版嵌套
  function index()
{

    $data['css']        = $this->config->item('css');
    $data['header']     = $this->load->view('header_view', '', TRUE);
    $this->load->view('basic_view', $data);
}
  注意,header_view视图是个单独的文件,比如header_view.jsp,因为要合拼到主JSP中,因此
$this->load->view中第3个参数是true;
 
6 表单辅助函数:
  $variable = '<input type="text" name="url" id="url" value="www.mysite.com" maxlength="100" size="50"

style="yellow" />';
可以变成这样了:
$this->load->helper('form');
   dropdownbox的话:
   $urlarray = array(
                   '1'         => 'www.this.com',
                   '2'         => 'www.that.com',
                   '3'         => 'www.theother.com'
                );

$variable = form_dropdown('url', $urlarray, '1');


7 设置验证规则:
  this->load->library('validation');

$rules['url']    = "required";
$rules['name']   = "required";

$this->validation->set_rules($rules);

  编写自己的规则:
$rules['name'] = "required|alpha|max_length[12]";意味着不能为空,字母,长度至少12个字符。你甚至能编写你

自己的规则。

  判断是否能通过验证:
  if ($this->validation->run() == FALSE)
{
    $this->load->view('myform');
}
else
{
    $this->load->view('success');
}

8 关闭错误
  把index.php中error_reporting (E_ALL);改为
error_reporting(0);

9 开启日志
   你需要设定权限确保 /system/logs目录是可读写的。 然后你在config文件中设定logging的级别:
$config['log_threshold'] = 4;


10 单元测试
   $this->load->library('unit_test');
  然后, 为每个测试准备三个变量:

$test—实际的测试内容, 一般是一个 PHP 表达式
$expected_result—你期待的结果
$test_name—你想要显示的测试名称
$test = floor(1.56);
$expected_result = 1;
$test_name = 'tests php floor function';
$this->unit->run($test, $expected_result, $test_name);
显示结果:
echo $this->unit->report();

11