一、基本结构
CodeIgniter3.0.0解压后有8个文件,分别是:
- application:项目文件
- system:系统(框架)文件,为方便升级,不建议修改
- user_guid:用户手册,不需要加入项目
- composer.json:composer配置文件,不需要加入项目
- contributing.md:如何参与项目贡献代码,不需要加入项目
- index.php:项目入口文件
- license.txt:许可文件,不需要加入项目
- readme.rst:说明文件,不需要加入项目
二、控制器Controller
控制器在application文件夹中的controllers文件夹中,默认控制器为welcome,有4小点需要注意:
1、控制器类名不需要加后缀
2、控制器文件名建议小写
3、控制器要直接或间接继承自CI_Controller类
4、可访问的action方法名不能以下划线开头,且访问权限要是public的
三、视图View
视图在application文件夹中的views文件夹中,几个tips:
1、在控制器中用如下代码加载views中的视图:
1//加载views文件夹中的user_index.php视图文件 2$this -> load -> view('user_index'); 3 4//加载views/user文件夹中的index.php视图文件 5$this -> load -> view('user/index');
2、在视图中,可以直接使用原生的php代码
3、在控制器中可以通过以下方式向视图中输出变量:
1<?php 2 class User extends CI_Controller { 3 public function index() { 4 $data = array( 5 'username' => 'jim', 6 'books' => array( 7 'a', 'b', 'c', 'd' 8 ) 9 ); 10 11 $head = array( 12 'title' => 'TITLE', 13 'subtitle' => 'SUB TITLE' 14 ); 15 16 $this -> load -> vars('data', $data); 17 //公共部分 18 $this -> load -> view('header'); 19 $this -> load -> view('user/index', $head); 20 } 21 } 22?>
在视图中用如下方式获取变量:
1<!DOCTYPE html> 2<html> 3<head> 4 <title>User</title> 5</head> 6<body> 7 <h1>User</h1> 8 <?php var_dump($data); ?> 9 <?php var_dump($title); ?> 10</body> 11</html>
四、CI的超级对象
CI中的超级对象即为当前的控制器对象。在视图中也可以直接使用$this访问超级对象。
超级对象提供了很多属性:
1、$this -> load ,装载器,主要提供了如下方法:
- view:装载视图
- vars:分配变量到视图
- database:装载数据库操作对象
- model:装载模型
- helper:加载帮助文件
2、$this -> uri,解析URL相关内容,主要提供如下方法:
segment:按索引(控制器为1)获取分段url值(类pathinfo没有key的模式:入口.php/控制器/动作/参数1/参数2。。。),示例如下
-
1 <?php 2 class User extends CI_Controller { 3 4 ///index.php/user/index/id/abc/name/god 5 public function index($param1, $param2) { 6 $param3 = $this -> uri -> segment(5); 7 var_dump(array( 8 'param1' => $param1, 9 'param2' => $param2, 10 'param3' => $param3 11 )); 12 /* 13 array (size=3) 14 'param1' => string 'id' (length=2) 15 'param2' => string 'abc' (length=3) 16 'param3' => string 'name' (length=4) 17 */ 18 $this -> load -> view('user/index'); 19 } 20 } 21 ?>
3、$this -> input,主要用于取post和server数据,使用方法如下:
1<?php 2 class User extends CI_Controller { 3 public function index() { 4 $username = $this -> input -> post('username'); 5 $ip = $this -> input -> server('REMOTE_ADDR'); 6 echo $ip;//127.0.0.1 7 $this -> load -> view('user/index'); 8 } 9 } 10?>
五、数据库操作
数据配置文件在 application/config/database.php 中。
1、查询示例
1<?php 2 class User extends CI_Controller { 3 public function index() { 4 $this -> load -> view('user/index'); 5 } 6 7 public function showusers() { 8 $this -> load -> database(); 9 $sql = 'select * from ci_test'; 10 $res = $this -> db -> query($sql); 11 $users = $res -> result(); 12 var_dump($users); 13 /* 14 array (size=1) 15 0 => 16 object(stdClass)[18] 17 public 'id' => string '1' (length=1) 18 public 'name' => string 'jim' (length=5) 19 public 'title' => string 'ci learn' (length=8) 20 */ 21 22 $users2 = $res -> result_array(); 23 /* 24 array (size=1) 25 0 => 26 array (size=3) 27 'id' => string '1' (length=1) 28 'name' => string 'atwal' (length=5) 29 'title' => string 'ci learn' (length=8) 30 */ 31 var_dump($users2); 32 33 $firstUser = $res -> row(); 34 var_dump($firstUser); 35 /* 36 object(stdClass)[18] 37 public 'id' => string '1' (length=1) 38 public 'name' => string 'atwal' (length=5) 39 public 'title' => string 'ci learn' (length=8) 40 */ 41 $this -> load -> view('user/show'); 42 } 43 } 44?>
要先调用 $this -> load -> database()进行装载数据库,然后才能使用 $this -> db 对象。
2、插入示例
1public function add() { 2 $this -> load -> database(); 3 $sql = "insert into swap_test(name,title) values ('jim', 'jim learn ci')"; 4 $bool = $this -> db -> query($sql); 5 if ($bool) { 6 //受影响行数 7 echo $this -> db -> affected_rows(); 8 9 //自增id 10 echo $this -> db -> insert_id(); 11 } 12}
3、参数绑定示例
为了安全,阻止SQL注入,建议用参数绑定的形式操作数据库。
1public function addsafe() { 2 //配置自动加载db 3 //application\config\autoload.php 4 //$autoload['libraries'] = array('database'); 5 //$this -> load -> database(); 6 7 $data[0] = 'lili'; 8 $data[1] = 'lili'; 9 $sql = "insert into swap_test(name,title) values (?,?)"; 10 $bool = $this -> db -> query($sql, $data); 11 if ($bool) { 12 //受影响行数 13 echo $this -> db -> affected_rows(); 14 15 //自增id 16 echo $this -> db -> insert_id(); 17 } 18}
4、表前缀
为了应对数据库表前缀变化,CI数据库配置(application\config\database.php)中有下面两项:
1$db['default'] = array( 2 'dbprefix' => 'ci_', 3 'swap_pre' => 'swap_', 4);
swap_pre的作用是,在代码中用swap_pre来替换dbprefix,可以达到换数据库表前缀不改代码的目地(即在代码中表前缀用swap_pre值就好)。
5、自动加载db对象
在每次数据库操作前都要加载database才可以使用db对象,显得比较麻烦,可以用CI中的自动加载能力简化这一步:
1//配置自动加载db 2//application\config\autoload.php 3//$autoload['libraries'] = array('database');
配置完后,就可以直接使用$this -> db 对象了。