轉(zhuǎn)載自地址:https://blog.csdn.net/admin_admin/article/details/51769805
CodeIgniter框架
1、回憶MVC
1.1、M:模型,提供數(shù)據(jù),保存數(shù)據(jù)
1.2、V:視圖,只負(fù)責(zé)顯示,表單form
1.3、C:控制器,協(xié)調(diào)模型和視圖
1.4、action:動(dòng)作,是控制器中的方法,用于被瀏覽器請(qǐng)求
2、CI中的MVC
CI簡(jiǎn)介:CodeIgniter是一個(gè)輕量級(jí)但功能強(qiáng)大的php框架,基于MVC的設(shè)計(jì)模式。提供了一套豐富的類庫(kù)
2.1、訪問的url使用的pathinfo
2.2、格式:入口文件/控制器/動(dòng)作(默認(rèn)pathinfo格式)
2.3、application目錄:
Controllers控制器
Models模型
Views視圖
2.4、默認(rèn)的控制器是welcome
2.5、默認(rèn)的動(dòng)作是index
2.6、文件名全部小寫
3、控制器(controller):
3.1、不需要加后綴,直接是類名.php
3.2、文件名全部小寫,例如 user.php(大寫的文件名瀏覽器不支持)
3.3、所有的控制器,直接或間接繼承自CI_Controller類,類名首字母大寫
3.4、方法名不區(qū)分大小寫
動(dòng)作要求:1.必須以public開頭
2.不能以下劃線(_)開頭
注意!方法名與類名相同,會(huì)被php當(dāng)做構(gòu)造方法(__construct)
4、視圖(view)
4.1、在控制器中加載視圖:$this->load->view(“user_add”);//不加擴(kuò)展名
4.2、在控制器中加載視圖:$this->load->view(“user/add”);//不加擴(kuò)展名
注意:可以多次調(diào)用$this->load->view(視圖);
4.3、視圖中,直接使用原生php代碼
4.4、在控制器中分配變量:$this->load->vars(“視圖中變量名”,php中變量名);
例如:$str=“abcdefg”;
$this->load->vars(“str”,$str);
視圖中使用:<?php echo $str;?>
4.5、在控制器中分配多個(gè)變量:
例如:$str=“abcd”;
$list = array(“id”=>”1”,”name”=>”zhangsan”,”pwd”=>”1234”);
$data[“str”]= $str;
$data[“l(fā)ist”]= $list;
$this->load->vars($data);
4.6、CI框架會(huì)自己解決php中的短標(biāo)簽<?=…?>
4.7、推薦使用:<?phpforeach($listas $key=>$val):?>…<?php endforeach;?>
5、超級(jí)對(duì)象:當(dāng)前控制器對(duì)象,提供了很多屬性
5.1、$this->load屬性
5.1.1、裝載器類的實(shí)例 system/core/Loader.php
5.1.2、CI_Loader提供的方法:
$this->load->view()裝載視圖
$this->load->vars()分配變量到視圖
$this->load->database()裝載數(shù)據(jù)庫(kù)操作對(duì)象
$this->load->model()裝載模型對(duì)象
$this->load->helper()幫助對(duì)象
$this->load->view(“視圖名”,$變量名)裝載視圖時(shí)分配變量
5.2、$this->uri屬性
5.2.1、裝載器類的實(shí)例 system/core/URI.php
5.2.2、CI_URI提供的方法:
$this->uri->Segment(n)用于獲取URL中的第n個(gè)參數(shù)
控制器代表1,動(dòng)作代表2,值1代表3,值2代表為4…
用法1:入口文件/控制器/動(dòng)作/值1/值2
echo $this->uri->segment(3);//輸出值1
用法2:index.php/控制器/動(dòng)作/6
//可以直接獲取;參數(shù)一定要對(duì)應(yīng)正確
publicfunction($id=0){
echo $id;//輸出6
}
5.3、$this->input屬性
5.3.1、裝載器類的實(shí)例 system/core/Input.php
5.3.2、CI_Input提供的方法:
$this->input->post(‘username’);//$_POST[“username”];
$this->input->server(“DOCUMENT_ROOT”);//$_SERVER[“DOCUMENT_ROOT”]
$this->input->server(“REMOTE_ADDR”);//客戶端IP
$this->input->server(“SERVER_ADDR”);//服務(wù)器端IP
注意:在視圖中,可以直接用$this來訪問超級(jí)對(duì)象中的屬性
6、訪問數(shù)據(jù)庫(kù)
6.1、修改配置文件:application/config/database.php
6.2、裝載數(shù)據(jù)庫(kù)操作類:$this->load->database();
加載成功后,會(huì)放入超級(jí)對(duì)象的屬性中,默認(rèn)屬性名是db
$this->db
6.3、$query= $this->db->query($sql);//返回值是對(duì)象(array_fetch_object)
$sql = $this->db->last_query($sql);//顯示最后一條執(zhí)行的sql語句
6.3.1、$query= $this->db->escape();//此函數(shù)會(huì)確定數(shù)據(jù)類型,對(duì)字符串類型數(shù)據(jù)轉(zhuǎn)義
6.4、$list= $query->result();//返回?cái)?shù)組,數(shù)組中是一個(gè)一個(gè)的對(duì)象
6.5、$list= $query->result_array();//返回二維數(shù)組,里面是關(guān)聯(lián)數(shù)組
6.6、$row= $query->row();//返回第一條數(shù)據(jù),直接是一個(gè)對(duì)象
6.7、$row= $query->row_array();//返回第一條數(shù)據(jù),是一個(gè)數(shù)組
6.8、$count= $query->num_rows();//返回結(jié)果集的行數(shù)
6.9、$field= $query->num_fields();//返回請(qǐng)求的字段數(shù)
6.10、$count= $query->affected_rows();//返回受影響的行數(shù)
6.11、$id= $query->insert_id();//返回自增ID
7、數(shù)據(jù)庫(kù)中的配置
7.1、自動(dòng)加載db:
在application/config/autoload.php中配置:
$autoload[“l(fā)ibraries”]= array(“database”);
這樣就不需要 $this->load->database();
7.2、參數(shù)綁定
$name = $this->input->post(“name”);
$pwd = $this->input->post(“pwd”);
//使用問號(hào)綁定參數(shù)
$data[0]= $name;
$data[1]= $pwd;//用二維數(shù)組傳遞值
$sql =“insertinto ci_user(name,pwd) values (?,?)”;//多個(gè)問號(hào),需要傳一個(gè)索引數(shù)組
$bool = $this->db->query($sql,$data);//返回值是boolean
7.3、表前綴配置
在application/config/database.php中配置:
$db[‘default’][‘dbprefix’]=‘ci_’;
$db[‘default’][‘swap_pre’]=‘ci_’;
配置為一樣,代碼中,直接編寫表前綴后面的名就行了,如果以后項(xiàng)目表前綴發(fā)生變化,只需要修改$db[‘default’][‘dbprefix’]=‘new_’,代碼中ci_會(huì)自動(dòng)替換為new_
8、數(shù)據(jù)庫(kù)操作(AR模型)
8.1、配置application/cinfig/database.php中
$active_record = TRUE;
8.2、在配置文件中,配置表前綴后,會(huì)自動(dòng)添加
8.3、查找(get):
$res = $this->db->get(‘表名’);//返回結(jié)果集對(duì)象
$list = $res->result();//返回?cái)?shù)組,數(shù)組中是一個(gè)一個(gè)的對(duì)象
$list = $res->result_array();//返回二維數(shù)組,里面是關(guān)聯(lián)數(shù)組
8.4、插入(insert):
//數(shù)據(jù)使用關(guān)聯(lián)數(shù)組
$data[“數(shù)據(jù)庫(kù)字段名”]=值;//$this->input->post(“name”);
$data[“數(shù)據(jù)庫(kù)字段名”]=值;
$res = $this->db->insert(“數(shù)據(jù)表名”,$data);//返回結(jié)果boolean;
8.5、修改(update):
//數(shù)據(jù)使用關(guān)聯(lián)數(shù)組
$data[“數(shù)據(jù)庫(kù)字段名”]=值;//$this->input->post(“name”);
$data[“數(shù)據(jù)庫(kù)字段名”]=值;
$where = array(“id”=>”4”);
$res = $this->db->update(“數(shù)據(jù)表名”,$data,$where);//返回結(jié)果boolean;
8.6、刪除(delete):
$where = array(“id”=>”4”);
$res = $this->db->dalete(“數(shù)據(jù)表名”, $where);//返回結(jié)果boolean;
9、AR的連貫操作
9.1、$res= $this->db->select("id,name")//查找的字段
->from("user")//數(shù)據(jù)表名
->where("id >",3)//條件 id與>之間必須要有空格
->order_by("id desc")//排序
->limit(3,1)//分頁;表示跳過1條,取3條,與tp相反
->get();//獲取數(shù)據(jù)
$list = $res->result_array();
9.2、where()條件
條件符:“>”、“>=”、“<”、“<=”、“=”、“!=“;不指定條件付默認(rèn)是”=“
1、只有單個(gè)條件時(shí):
9.2.1、$res= $this->db->where(“字段”,”值”)->get();
例如:$res= $this->db->where(“name”,”admin”)->get();//自動(dòng)會(huì)加上“=”
9.2.2、$res= $this->db->where(“字段=”,”值”)->get();
例如:$res= $this->db->where(“name=”,”admin”)->get();//必須要有空格
2、有多個(gè)條件時(shí):用array()
9.2.3、$res= $this->db->where(array(“字段”=>”值”,“字段”=>”值”))->get();
例如:$res= $this->db->where(array(“name”=>”admin”,“id>3”=>”2”))->get();
9.2.4、復(fù)雜的查詢語句使用$this->db->query($sql,$data);//使用問號(hào)綁定參數(shù)
9.3、join連接查詢
9.3.1、默認(rèn)為左查詢(left join … on)
$this->db->select(“字段”)
->from(“數(shù)據(jù)表名”)
->join('連接表','連接條件')
->get();? ? //默認(rèn)為左查詢(left join … on) 例如:
$this->db->select(“*”)
->from(“user”)
->join('category', user.id= category.id)//默認(rèn)為左查詢(left join )
->get();
9.3.2、通過join()第三個(gè)可選參數(shù)選擇連接,可選參數(shù)包括:left、 right、outer、 inner、left outer、right outer
例如:
$this->db->select(“*”)
->from(“user”)
->join(“category”, “user.id = category.id”,”left”)//通過第三個(gè)參數(shù)指定
->get();
10、擴(kuò)展CI控制器
10.1、擴(kuò)展控制器
1、在application/core新建一個(gè)自己的控制器(MY_Controller),新建的控制器繼承CI_Controller類,可以從自己創(chuàng)建的控制器中進(jìn)行擴(kuò)展。
例如:class MY_Controller extends CI_Controller{
Public function __consreuct(){
Parent::__construct(); //調(diào)用父類的構(gòu)造方法
//登陸驗(yàn)證
//權(quán)限驗(yàn)證
}
}
2、讓welcome.php繼承自己創(chuàng)建的控制器(MY_Controller),間接繼承CI_Controller。
3、控制器前綴可以修改
在application/config/config.php中修改:
$config['subclass_prefix'] = 'MY_';
11、模型(model)
11.1、模型文件命名:user_model.php 、category_model.php,文件名小寫
建議:模型文件名使用_model為后綴,防止與控制器類名沖突!
11.2、所有的模型,直接或間接繼承自CI_Model類,類名首字母大寫
11.3、里面創(chuàng)建所需要的方法:getAll()…
11.4、控制器中加載模型:$this->load->model(“模型文件名”); //不加后綴
11.5、控制器中加載模型時(shí)起別名:$this->load->model(“模型文件名”,”別名”);
加載成功后,會(huì)放入超級(jí)對(duì)象的屬性中,默認(rèn)屬性名是模型文件名或別名
11.6、控制器中調(diào)用模型,獲取數(shù)據(jù):$this->模型文件名->方法名();
例如:$this->load->model(“User_model”); //注意大小寫
$this->User_model->getAll(); //調(diào)用模型,獲取數(shù)據(jù)
11.6.1、控制器中調(diào)用模型,獲取數(shù)據(jù):$this->別名->方法名();
例如:$this->load->model(“User_model”,”user”); //注意大小寫
$this->user->getAll(); //調(diào)用模型,獲取數(shù)據(jù)
12、CI中url相關(guān)函數(shù)
12.1、加載輔助函數(shù):$this->load->helper(“url”);
配置自動(dòng)加載輔助函數(shù):在application/config/autoload.php中配置
$autoload['helper'] = array('url');
就不用寫$this->load->helper(“url”);
輔助函數(shù):
site_url(“控制器/動(dòng)作/參數(shù)”) ; //處理url及參數(shù)
在視圖中使用<?php echo site_url(“控制器/動(dòng)作/參數(shù)”)?>
base_url();//返回網(wǎng)站的根目錄(“ci/”);
13、CI中的路由與偽靜態(tài)、隱藏index.php入口文件
13.1、路由設(shè)置
在application/config/routes.php中設(shè)置/修改:
$route['default_controller'] = "welcome"; //默認(rèn)的控制器為welcome
13.2、偽靜態(tài)設(shè)置
在application/config/routes.php中設(shè)置/修改:
//正則匹配(控制器/)
$route['news/[\d]{6}/([\d\w]+)\.html'] = 'user/show/$1';
http://localhost/CI/index.php/news/201401/caolizhi66.html
將動(dòng)作news中的參數(shù)路由到user/show/$1
13.3、隱藏入口文件index.php
13.3.1、開啟apache配置文件:LoadModule rewrite_module modules/mod_rewrite.so
13.3.2、在入口文件統(tǒng)計(jì)目錄中,創(chuàng)建一個(gè).htaccess文件,內(nèi)容如下:
<IFModule mod_rewrite.c>
RewriteEngine on //啟用偽靜態(tài)
RewriteCond %{REQUEST_FILENAME} !-d //匹配不存在的目錄
RewriteCond %{REQUEST_FILENAME} !-f //匹配不存在的文件
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] //重寫規(guī)則
</IFModule>
這樣就不用寫入口文件index.php了
14、CI中的分頁
$this->load->library('pagination');
1 $config['base_url'] = $url;
2 /* 分頁的基礎(chǔ) URL
3 如果你想用a、b的鏈接形式,則該url應(yīng)該形式如/news/page/
4 如果鏈接是c、d的形式,則url應(yīng)該如/news?
5 */
6 $config['total_rows'] = $total;//記錄總數(shù),這個(gè)沒什么好說的了,就是你從數(shù)據(jù)庫(kù)取得記錄總數(shù)
7 $config['per_page'] = $pagesize; //每頁條數(shù)。額,這個(gè)也沒什么好說的。。自己設(shè)定。默認(rèn)為10好像。
8 $config['page_query_string'] = TRUE;
9 /*傳參形式。開啟true則會(huì)自動(dòng)在你的url后面加上&per_page=3。(這個(gè)per_page是默認(rèn)的查詢字符,當(dāng)然你也可以用$config['query_string_segment']來自己設(shè)定)
10 因此c、d中的形式一般是為localhost/news?&per_page=2不過都一樣,沒什么影響。get的per_page還是3
11 */
12 $config['first_link'] = '首頁'; // 第一頁顯示
13 $config['last_link'] = '末頁'; // 最后一頁顯示
14 $config['next_link'] = '下一頁>'; // 下一頁顯示
15 $config['prev_link'] = '<上一頁'; // 上一頁顯示
16 $config['cur_tag_open'] = '<aclass="current">'; // 當(dāng)前頁開始樣式
17 $config['cur_tag_close'] = '</a>';
18 /*當(dāng)前頁結(jié)束樣式。這些你可以自己嘗試一下。
19比如說我想讓當(dāng)前頁的分頁數(shù)字樣式好看一點(diǎn),紅色字體等。你就可以在current上加上css代碼
20*/
21 $config['num_links'] = 2;//當(dāng)前連接前后顯示頁碼個(gè)數(shù)。意思就是說你當(dāng)前頁是第5頁,那么你可以看到3、4、5、6、7頁。
22 $config['uri_segment']=4;
23/*這個(gè)是你在用a)、b)鏈接樣式的時(shí)候,用來判斷頁頁數(shù)。
24 比如localhost/news/page/3 這個(gè)uri_segment就要設(shè)定為3。localhost/news/title/page/3這個(gè)就要設(shè)定為4
25 */
26 $config['use_page_numbers']= TRUE;
27/*這個(gè)就是a)、b)的差別了。開啟了,page就會(huì)表示頁數(shù)。false就會(huì)表示記錄數(shù)
15.定義常量
? ? constants.php
? ? //自定義常量? define('GATEWAY_URL', 'http://pay.bebanks.com');