一、基本结构

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中的视图:

//加载views文件夹中的user_index.php视图文件
$this -> load -> view('user_index'); //加载views/user文件夹中的index.php视图文件
$this -> load -> view('user/index');

2、在视图中,可以直接使用原生的php代码

3、在控制器中可以通过以下方式向视图中输出变量:

<?php
class User extends CI_Controller {
public function index() {
$data = array(
'username' => 'jim',
'books' => array(
'a', 'b', 'c', 'd'
)
); $head = array(
'title' => 'TITLE',
'subtitle' => 'SUB TITLE'
); $this -> load -> vars('data', $data);
//公共部分
$this -> load -> view('header');
$this -> load -> view('user/index', $head);
}
}
?>

在视图中用如下方式获取变量:

<!DOCTYPE html>
<html>
<head>
<title>User</title>
</head>
<body>
<h1>User</h1>
<?php var_dump($data); ?>
<?php var_dump($title); ?>
</body>
</html>

四、CI的超级对象

CI中的超级对象即为当前的控制器对象。在视图中也可以直接使用$this访问超级对象。

超级对象提供了很多属性:

1、$this -> load ,装载器,主要提供了如下方法:

  • view:装载视图
  • vars:分配变量到视图
  • database:装载数据库操作对象
  • model:装载模型
  • helper:加载帮助文件

2、$this -> uri,解析URL相关内容,主要提供如下方法:

  segment:按索引(控制器为1)获取分段url值(类pathinfo没有key的模式:入口.php/控制器/动作/参数1/参数2。。。),示例如下

  • <?php
    class User extends CI_Controller { ///index.php/user/index/id/abc/name/god
    public function index($param1, $param2) {
    $param3 = $this -> uri -> segment(5);
    var_dump(array(
    'param1' => $param1,
    'param2' => $param2,
    'param3' => $param3
    ));
    /*
    array (size=3)
    'param1' => string 'id' (length=2)
    'param2' => string 'abc' (length=3)
    'param3' => string 'name' (length=4)
    */
    $this -> load -> view('user/index');
    }
    }
    ?>

3、$this -> input,主要用于取post和server数据,使用方法如下:

<?php
class User extends CI_Controller {
public function index() {
$username = $this -> input -> post('username');
$ip = $this -> input -> server('REMOTE_ADDR');
echo $ip;//127.0.0.1
$this -> load -> view('user/index');
}
}
?>

五、数据库操作

数据配置文件在 application/config/database.php 中。

1、查询示例

<?php
class User extends CI_Controller {
public function index() {
$this -> load -> view('user/index');
} public function showusers() {
$this -> load -> database();
$sql = 'select * from ci_test';
$res = $this -> db -> query($sql);
$users = $res -> result();
var_dump($users);
/*
array (size=1)
0 =>
object(stdClass)[18]
public 'id' => string '1' (length=1)
public 'name' => string 'jim' (length=5)
public 'title' => string 'ci learn' (length=8)
*/ $users2 = $res -> result_array();
/*
array (size=1)
0 =>
array (size=3)
'id' => string '1' (length=1)
'name' => string 'atwal' (length=5)
'title' => string 'ci learn' (length=8)
*/
var_dump($users2); $firstUser = $res -> row();
var_dump($firstUser);
/*
object(stdClass)[18]
public 'id' => string '1' (length=1)
public 'name' => string 'atwal' (length=5)
public 'title' => string 'ci learn' (length=8)
*/
$this -> load -> view('user/show');
}
}
?>

要先调用 $this -> load -> database()进行装载数据库,然后才能使用 $this -> db 对象。

2、插入示例

public function add() {
$this -> load -> database();
$sql = "insert into swap_test(name,title) values ('jim', 'jim learn ci')";
$bool = $this -> db -> query($sql);
if ($bool) {
//受影响行数
echo $this -> db -> affected_rows(); //自增id
echo $this -> db -> insert_id();
}
}

3、参数绑定示例

为了安全,阻止SQL注入,建议用参数绑定的形式操作数据库。

public function addsafe() {
//配置自动加载db
//application\config\autoload.php
//$autoload['libraries'] = array('database');
//$this -> load -> database(); $data[0] = 'lili';
$data[1] = 'lili';
$sql = "insert into swap_test(name,title) values (?,?)";
$bool = $this -> db -> query($sql, $data);
if ($bool) {
//受影响行数
echo $this -> db -> affected_rows(); //自增id
echo $this -> db -> insert_id();
}
}

4、表前缀

为了应对数据库表前缀变化,CI数据库配置(application\config\database.php)中有下面两项:

$db['default'] = array(
'dbprefix' => 'ci_',
'swap_pre' => 'swap_',
);

swap_pre的作用是,在代码中用swap_pre来替换dbprefix,可以达到换数据库表前缀不改代码的目地(即在代码中表前缀用swap_pre值就好)。

5、自动加载db对象

在每次数据库操作前都要加载database才可以使用db对象,显得比较麻烦,可以用CI中的自动加载能力简化这一步:

//配置自动加载db
//application\config\autoload.php
//$autoload['libraries'] = array('database');

配置完后,就可以直接使用$this -> db 对象了。

最新文章

  1. canvas中的rotate的使用方法
  2. AP创建会计科目
  3. BZOJ1742[Usaco2005 nov]Grazing on the Run
  4. Java 引用
  5. 无需cygwin,使用NDK进行开发
  6. Spring 定时任务的配置
  7. map 后 PE 蓝屏原因专题讨论(e820cycles参数)
  8. poj3669 Meteor Shower(BFS)
  9. static 静态代码块 动态代码块 单例
  10. 网页元素定位神器之Xpath详解
  11. ubuntu 备份安装程序列表
  12. Contoso 大学 - 3 - 排序、过滤及分页
  13. GPUImage 滤镜头文件
  14. C#学习日志 day 5 plus------ interface 数组及stringBuilder相关
  15. QT中代码中与设计器中控件信号与SLOT连接(原来还可以这样连接)
  16. Django REST framework基础:版本控制
  17. BootStrap常用组件及响应式开发
  18. ajax 删除数据无刷新
  19. 20170803 Airflow自带的API进行GET 和POST动作部分内容
  20. JAVA各种系统架构图及其简介

热门文章

  1. java文件
  2. linux df -h显示空间信息不正确
  3. HTTP Method 详细解读(`GET` `HEAD` `POST` `OPTIONS` `PUT` `DELETE` `TRACE` `CONNECT`)--转
  4. java导入、导出
  5. 旧文备份:安装cygwin及在console下输入和显示中文
  6. 微信端H5页面问题总结
  7. JQuery 过滤选择器 与属性修改的方法演示比较
  8. iOS之旅--隐藏(去除)导航栏底部横线
  9. Windows Server 2012 搭建DHCP及远程路由访问
  10. Java OOP——第五章 异常