之前的地方用tp3.2,现在用ci2.2。在学框架的路上……渐行渐远
一.简介:
1.mvc框架:model(模型),controller(控制器),view(视图)。
wtf,到底mvc是什么:model层就是跟数据库打交道的(一般还会有service层,将从数据库获得的数据传入业务逻辑处理),controller层控制请求的,view层就是html或php页面,用来显示的。
2.干净的url
如果你学过thinkphp,对pathinfo模式肯定不陌生,ci的url就是这种
http://example.com/[控制器类名]/[控制器方法名]/[所需参数]
3.未完
二.应用程序流程图
- index.php 作为前端控制器,初始化运行 CodeIgniter 所需要的基本资源。
- Router 检查 HTTP 请求,以确定谁来处理请求。
- 如果缓存(Cache)文件存在,它将绕过通常的系统执行顺序,被直接发送给浏览器。
- 安全(Security)。应用程序控制器(Application Controller)装载之前,HTTP 请求和任何用户提交的数据将被过滤。
- 控制器(Controller)装载模型、核心库、辅助函数,以及任何处理特定请求所需的其它资源。
- 最终视图(View)渲染发送到 Web 浏览器中的内容。如果开启缓存(Caching),视图首先被缓存,所以将可用于以后的请求。
三.简单的crud例子
1.连接数据库
public function __construct(){
$this->load->database();
}
2.查
public function get_news($cond){
if($cond == NULL) {
$query = $this->db->get("t_eason_news");
}else{
$query = $this->db->like("title",$cond)->get("t_eason_news");
}
return $query->result();
}
$this->db->get("t_eason_news");//t_eason_news表名。返回$query是查寻结果(包含其他信息)
通过result()或rsult_array()返回对象或数组形式的结果集
3.改
public function edit_news($id,$title,$text){
$data = array(
'title' => $title,
'text' => $text
);
$this->db->where('id',$id);
$result = $this->db->update('t_eason_news',$data);
return $result;
}
4.增
public function add_news($title,$text){
$data = array(
'title' => $title,
'text' => $text
);
$result = $this->db->insert('t_eason_news',$data);
return $result;
}
5.删
public function delete_news($id){
$this->db->where('id',$id);
$result = $this->db->delete('t_eason_news');
return $result;
}