thinkphp 是一個免費(fèi)開源的,快速、簡單的面向?qū)ο蟮?輕量級php開發(fā)框架 ,創(chuàng)立于2006年初,遵循apache2開源協(xié)議發(fā)布,是為了敏捷web應(yīng)用開發(fā)和簡化企業(yè)應(yīng)用開發(fā)而誕生的。thinkphp從誕生以來一直秉承簡潔 實(shí)用的設(shè)計(jì)原則,在保持出色的性能和至簡的代碼的同時,也注重易用性。并且擁有眾多的原創(chuàng)功能和特性,在社區(qū)團(tuán)隊(duì)的積極參與下,在易用性、擴(kuò)展性和性能方 面不斷優(yōu)化和改進(jìn),已經(jīng)成長為國內(nèi)最領(lǐng)先和最具影響力的web應(yīng)用開發(fā)框架,眾多的典型案例確??梢苑€(wěn)定用于商業(yè)以及門戶級的開發(fā)。
thinkphp基于mvc的php框架
m – model 模型 工作:負(fù)責(zé)數(shù)據(jù)的操作
v – view 視圖(模板) 工作:負(fù)責(zé)前臺頁面顯示
c – controller 控制器(模塊) 工作:描述功能
thinkphp核心文件介紹
├─thinkphp.php 框架入口文件
├─common 框架公共文件
├─conf 框架配置文件
├─extend 框架擴(kuò)展目錄
├─lang 核心語言包目錄
├─lib 核心類庫目錄
│ ├─behavior 核心行為類庫
│ ├─core 核心基類庫
│ ├─driver 內(nèi)置驅(qū)動
│ │ ├─cache 內(nèi)置緩存驅(qū)動
│ │ ├─db 內(nèi)置數(shù)據(jù)庫驅(qū)動
│ │ ├─taglib 內(nèi)置標(biāo)簽驅(qū)動
│ │ └─template 內(nèi)置模板引擎驅(qū)動
│ └─template 內(nèi)置模板引擎
└─tpl 系統(tǒng)模板目錄
#項(xiàng)目目錄結(jié)構(gòu)及說明:
home 前臺應(yīng)用文件夾
├─common 項(xiàng)目公共文件目錄
├─conf 項(xiàng)目配置目錄
├─lang 項(xiàng)目語言目錄
├─lib 項(xiàng)目類庫目錄
│ ├─action action類庫目錄
│ ├─behavior 行為類庫目錄
│ ├─model 模型類庫目錄
│ └─widget widget類庫目錄
├─runtime 項(xiàng)目運(yùn)行時目錄
│ ├─cache 模板緩存目錄
│ ├─data 數(shù)據(jù)緩存目錄
│ ├─logs 日志文件目錄
│ └─temp 臨時緩存目錄
└─tpl 項(xiàng)目模板目錄
thinkphp 3 mvc模式和url訪問
什么是mvc
m -model 編寫model類 對數(shù)據(jù)進(jìn)行操作
v -view 編寫html文件,頁面呈現(xiàn)
c -controller 編寫類文件(useraction.class.php)
thinkphp的mvc特點(diǎn)
編寫非常靈活,只有view都可以執(zhí)行
thinkphp的mvc對應(yīng)的目錄
m 項(xiàng)目目錄/應(yīng)用目錄/lib/model
v 項(xiàng)目目錄/應(yīng)用目錄/tpl
c 項(xiàng)目目錄/應(yīng)用目錄/lib/action
url訪問c
url的4種訪問方式
1.pathinfo 模式
http://域名/項(xiàng)目名/入口文件/模塊名/方法名/鍵1/值1/鍵2/值2
2.普通模式
http://域名/項(xiàng)目名/入口文件?m=模塊名&a=方法名&鍵1=值1&鍵2=值2
3.rewrite模式
http://域名/項(xiàng)目名/模塊名/方法名/鍵1/值1/鍵2/值2
4.兼容模式
http://域名/項(xiàng)目名/入口文件?s=模塊名/方法名/鍵1/值1/鍵2/值2
thinkphp 3.1.2 輸出和模型使用
thinkphp 3 的輸出
a、通過 echo 等php原生的輸出方式在頁面中輸出
b、通過display方法輸出
想分配變量可以使用assign方法
c、修改左右定界符
休要修改配置文件中的配置項(xiàng)
‘tmpl_l_delim’=>'<{‘, //修改左定界符
‘tmpl_r_delim’=>’}>’, //修改右定界符
thinkphp 3 的模型使用
需要在方法中通過new model(表名)的形式操作數(shù)據(jù)庫
$m=new model(‘user’);
$arr=$m->select();
‘db_type’=>’mysql’, //設(shè)置數(shù)據(jù)庫類型
‘db_host’=>’localhost’,//設(shè)置主機(jī)
‘db_name’=>’thinkphp’,//設(shè)置數(shù)據(jù)庫名
‘db_user’=>’root’, //設(shè)置用戶名
‘db_pwd’=>”, //設(shè)置密碼
‘db_port’=>’3306′, //設(shè)置端口號
‘db_prefix’=>’tp_’, //設(shè)置表前綴
也可以使用dsn方法進(jìn)行配置
‘db_dsn’=>’mysql://root:@localhost:3306/thinkphp’,//使用dsn方式配置數(shù)據(jù)庫信息
如果兩種方式同時存在,以dsn方式為優(yōu)先
還有一種簡單實(shí)用模型的方式
m() 等效為 new model();
$m=m(‘user’);
$arr=$m->select();
使用模型的實(shí)例可以對數(shù)據(jù)進(jìn)行操作,操作的工作一般就是對數(shù)據(jù)庫進(jìn)行 增刪改查 curd
增 -c create $m->add()
刪 -d delete $m->delete()
改 -u update $m->save()
查 -r read $m->select()
a、模板可以遍歷數(shù)組
1
2
3
<volist name='data' id='vo'>
<{$vo.id}>----<{$vo.username}>-----<{$vo.sex}><br/>
</volist>
b、我們可以開啟調(diào)試功能中的page_trace
1.開啟調(diào)試功能
//3.開啟調(diào)試模式(在主入口文件配置index.php)
define(‘a(chǎn)pp_debug’,true);
2.我們需要設(shè)置配置文件,開啟頁面trace
‘show_page_trace’=>true,//開啟頁面trace,需要有$this->display()才顯示
curd特性
讀取數(shù)據(jù)
對數(shù)據(jù)的讀取 read
$m=new model(‘user’);
$m=m(‘user’);
select
$m->select();//獲取所有數(shù)據(jù),以數(shù)組形式返回
find
$m->find(2);//獲取單條數(shù)據(jù)
getfield(字段名)//獲取一個具體的字段值
$arr=$m->where(‘id=2′)->getfield(‘username’);
創(chuàng)建數(shù)據(jù)
對數(shù)據(jù)的添加 create
$m=new model(‘user’);
$m=m(‘user’);
$m->字段名=值
$m->add();
返回值是新增的id號
刪除數(shù)據(jù)
$m=m(‘user’);
$m->delete(2); //刪除id為2的數(shù)據(jù)
$m->where(‘id=2′)->delete(); //與上面效果相同,也是刪除id為2的數(shù)據(jù)
返回值是受影響行數(shù)
更新數(shù)據(jù)
$m=m(‘user’);
$data['id']=1;
$data['username']=’ztz2′;
$m->save($data);
返回值是受影響行數(shù)
查詢方式
普通查詢方式
a、字符串
$arr=$m->where(“sex=0 and username=’gege'”)->find();
b、數(shù)組
$data['sex']=0;
$data['username']=’gege';
$arr=$m->where($data)->find();
注意:這種方式默認(rèn)是and的關(guān)系,如果使用or關(guān)系,需要添加數(shù)組值
$data['sex']=0;
$data['username']=’gege';
$data['_logic']=’or';
表達(dá)式查詢方式
$data['id']=array(‘lt’,6);
$arr=$m->where($data)->select();
eq 等于
neq不等于
gt 大于
egt大于等于
lt 小于
elt小于等于
like 模糊查詢
$data['username']=array(‘like’,’%ge’);
$arr=$m->where($data)->select();
notlike
$data['username']=array(‘notlike’,’%ge%’); //notlike中間沒有空格
$arr=$m->where($data)->select();
注意:如果一個字段要匹配多個通配符
$data['username']=array(‘like’,array(‘%ge%’,’%2%’,’%五%’),’and’);//如果沒有第三個值,默認(rèn)關(guān)系是or關(guān)系
$arr=$m->where($data)->select();
between
$data['id']=array(‘between’,array(5,7));
$arr=$m->where($data)->select();
//select * from tp_user where ( (id between 5 and 7 ) )
$data['id']=array(‘not between’,array(5,7));//注意,not 和 between中間一定要有空格
$arr=$m->where($data)->select();
in
$data['id']=array(‘in’,array(4,6,7));
$arr=$m->where($data)->select();
//select * from tp_user where ( id in (4,6,7) )
$data['id']=array(‘not in’,array(4,6,7));
$arr=$m->where($data)->select();
//select * from tp_user where ( id not in (4,6,7) )
區(qū)間查詢
$data['id']=array(array(‘gt’,4),array(‘lt’,10));//默認(rèn)關(guān)系是 and 的關(guān)系
//select * from tp_user where ( (id > 4) and (id < 10) )
$data['id']=array(array(‘gt’,4),array(‘lt’,10),’or’) //關(guān)系就是or的關(guān)系
$data['name']=array(array(‘like’,’%2%’),array(‘like’,’%五%’),’gege’,’or’);
統(tǒng)計(jì)查詢
count //獲取個數(shù)
max //獲取最大數(shù)
min //獲取最小數(shù)
avg //獲取平均數(shù)
sum //獲取總和
sql直接查詢
a、query 主要數(shù)處理讀取數(shù)據(jù)的
成功返回?cái)?shù)據(jù)的結(jié)果集
失敗返回boolean false
$m=m();
$result=$m->query(“select * from t_user where id >50″);
var_dump($result);
b、execute 用于更新個寫入操作
成功返回影響行數(shù)
失敗返回boolean false
$m=m();
$result=$m->execute(“insert into t_user(username) values(‘ztz3′)”);
var_dump($result);
連貫操作
常用連貫操作
1.where
幫助我們設(shè)置查詢條件
2.order
對結(jié)果進(jìn)行排序
$arr=$m->order(‘id desc’)->select();
$arr=$m->order(array(‘id’=>’desc’,’sex’=>’asc’))->select();
3.limit
限制結(jié)果
limit(2,5)
limit(‘2,5′)
limit(10)//limit(0,10)
4.field
設(shè)置查詢字段
field(‘username as name,id’)
field(array(‘username’=>’name’,’id’)
field(‘id’,true) //獲取除了id以外的所有字段
5.table
設(shè)置表名
6.group
分組
7.having
alias 用于給當(dāng)前數(shù)據(jù)表定義別名 字符串
page 用于查詢分頁(內(nèi)部會轉(zhuǎn)換成limit) 字符串和數(shù)字
join* 用于對查詢的join支持 字符串和數(shù)組
union* 用于對查詢的union支持 字符串、數(shù)組和對象
distinct 用于查詢的distinct支持 布爾值
lock 用于數(shù)據(jù)庫的鎖機(jī)制 布爾值
cache 用于查詢緩存 支持多個參數(shù)(以后在緩存部分再詳細(xì)描述)
relation 用于關(guān)聯(lián)查詢(需要關(guān)聯(lián)模型擴(kuò)展支持) 字符串
validate 用于數(shù)據(jù)自動驗(yàn)證 數(shù)組
auto 用于數(shù)據(jù)自動完成 數(shù)組
filter 用于數(shù)據(jù)過濾 字符串
scope* 用于命名范圍 字符串、數(shù)組
視圖
模板的使用
a、規(guī)則
模板文件夾下[tpl]/[分組文件夾/][模板主題文件夾/]和模塊名同名的文件夾[index]/和方法名同名的文件[index].html(.tpl)
更換模板文件的后綴名(修改配置文件)
‘tmpl_template_suffix’=>’.html’,//更改模板文件后綴名
b、修改模板文件目錄層次
‘tmpl_file_depr’=>’_’,//修改模板文件目錄層次
c、模板主題
‘default_theme’=>’your’,//設(shè)置默認(rèn)模板主題
需要在tpl下面新建一個your文件夾作為模板主題文件夾
如何動態(tài)修改模板主題?
1、在后臺準(zhǔn)備一個功能,修改config.php文件中的默認(rèn)模板項(xiàng)
2、通過url傳遞 t=主題 參數(shù)可以修改不同的模板
‘default_theme’=>’your’,//設(shè)置默認(rèn)模板主題
‘tmpl_detect_theme’=>true,//自動偵測模板主題
‘theme_list’=>’your,my’,//支持的模板主題列表
輸出模板內(nèi)容
a、display
1.display中沒有參數(shù)
$this->display();
2.可以帶參數(shù)
$this->display(本模塊文件夾下的其他模板文件);
$this->display(‘index2′);
$this->display(其他文件夾下的模板文件);
$this->display(‘public:error’);//注意,僅僅需要在tpl下有public文件夾以及其中的error.html即可,不需要一定有public模塊
$this->display(其他主題下的 文件夾下的 模板文件);//需要開啟主題支持
$this->display(‘my:index:index’);
$this->display(一個url路徑);
$this->display(‘./public/error.html’);
$this->display(‘./public/error.html’,’utf-8′,’text/xml’);
$this->show($content);
3.fetch方法
獲得模板文件中的內(nèi)容,以字符串形式返回
$content=$this->fetch(‘public:error’);
4.show方法
不需要模板文件,可以直接輸出模板內(nèi)容
$content=$this->fetch(‘public:error’);
dump($content);
$content=str_replace(‘h1′,’i’,$content);
$this->show($content);
模板中的賦值
//$this->assign(‘name’,’趙桐正’);
$this->name=’趙桐正2′;
$this->display();
模板替換
__public__:會被替換成當(dāng)前網(wǎng)站的公共目錄 通常是 /public/
__root__: 會替換成當(dāng)前網(wǎng)站的地址(不含域名)
__app__: 會替換成當(dāng)前項(xiàng)目的url地址 (不含域名)
__group__:會替換成當(dāng)前分組的url地址 (不含域名)
__url__: 會替換成當(dāng)前模塊的url地址(不含域名)
__action__:會替換成當(dāng)前操作的url地址 (不含域名)
__self__: 會替換成當(dāng)前的頁面url
更換模板變量規(guī)則,修改配置項(xiàng)
‘tmpl_parse_string’=>array( //添加自己的模板變量規(guī)則
‘__css__’=>__root__.’/public/css’,
‘__js__’=>__root__.’/public/js’,
),
模板中的變量
變量輸出
1.標(biāo)量輸出
2.數(shù)組輸出
{$name[1]} //索引數(shù)組
{$name['k2']} //關(guān)聯(lián)數(shù)組
{$name.k1}
3.對象輸出
{$name:k}
{$name->k}
系統(tǒng)變量
{$think.get.id}
使用函數(shù)
{$name|strtoupper} 生成的編譯后文件是 <?php echo (strtoupper($name)); ?>
{$name|date=’y m d h:i:s’,###}
默認(rèn)值
{$name|default=’這里是默認(rèn)值’}
運(yùn)算符
+ – * / % ++ —
{$name++}
模板中的基本語法
導(dǎo)入css和js文件
1、css link
js scr
<link rel=’stylesheet’ type=’text/css’ href=’__public__/css/test.css’/>
<script src=’__public__/js/test.js’></script>
2.import
<import type=’js’ file=’js.test’ /> //導(dǎo)入public文件夾下面的js目錄中的test.js文件,import標(biāo)簽可以省略type屬性,默認(rèn)就是js的
<import type=’css’ file=’css.test’ />
//可以更改默認(rèn)文件夾 設(shè)置basepath屬性
<import type=’js’ file=’js.my’ basepath=’./other’/>
3.load
//方法可以自動檢測導(dǎo)入的文件類型
<load href=’__public__/js/test.js’ />
分支結(jié)構(gòu)
1、if
<if condition=’$sex eq “男”‘>
男人是泥巴做的
<else />
女人是水做的
</if>
<if condition=’$age lt 18′>
未成年
<elseif condition=’$age eq 18’/>
青春年少
<else />
成年
</if>
> gt
< lt
== eq
<= elt
>= egt
!= neq
=== heq
!== nheq
<switch name=’number’>
<case value=’1′>一個和尚挑水吃</case>
<case value=’2′>兩個和尚臺水吃</case>
<case value=’3′>三個和尚沒水吃</case>
<default/> 這里是默認(rèn)值
</switch>
循環(huán)結(jié)構(gòu)
1.for
<table border=’1′ width=’500′>
<for start=’10’ end=’00’ name=’j’ step=’-2′ comparison=’gt’>
<tr><td>{$j}</td><td>abc</td></tr>
</for>
</table>
2.volist
<volist name=’list’ id=’v’>
{$v.username}<br/>
</volist>
3.foreach
<foreach name=’list’ item=’v’ key=’k’>
{$k}——-{$v}<br/>
</foreach>
特殊標(biāo)簽
1、比較標(biāo)簽
eq或者 equal 等于
neq 或者notequal 不等于
gt 大于
egt 大于等于
lt 小于
elt 小于等于
heq 恒等于
nheq 不恒等于
2.范圍標(biāo)簽
in
<in name=’n’ value=’9,10,11,12′>在這些數(shù)字里面<else/>不在這些數(shù)字的范圍內(nèi)</in>
<notin name=’n’ value=’9,10,11,12′>在這些數(shù)字里面<else/>不在這些數(shù)字的范圍內(nèi)</in>
between
<notbetween name=’n’ value=’1,10′>{$n}在1-10之間<else/>{$n}不在1到10之間</between>
3.present
標(biāo)簽來判斷模板變量是否已經(jīng)賦值,
<present name=’m’>m有賦值<else/>m沒有賦值</present>
4.empty
empty標(biāo)簽判斷模板變量是否為空,
<empty name=’n’>n為空賦值<else/>n有值</empty>
5.defined
判斷常量是否已經(jīng)定義
6.define
在模板中定義常量
7.assing
模板中變量賦值
其他標(biāo)簽使用
1、在模板中直接使用php代碼
<php> echo “我是趙桐正” </php>
2、建議更改左右定界符
在配置文件中改變
‘tmpl_l_delim’=>'<{‘, //修改左定界符
‘tmpl_r_delim’=>’}>’, //修改右定界符
模板的使用技巧
模板包含
<include file=”完整模板文件名” />
<include file=”./tpl/default/public/header.html” />
<include file=”read” />
<include file=”public:header” />
<include file=”blue:user:read” />
<include file=”$tplname” />
<include file=”header” title=”thinkphp框架”keywords=”開源web開發(fā)框架”/>
在模板中變量用[變量]接受
<include file=’file1,file2′ />
模板渲染
1、自動開啟模板渲染 設(shè)置配置文件
‘layout_on’=>true,//開啟模板渲染
準(zhǔn)備一個模板渲染頁面,在頁面中使用{__content__}接受具體模板頁面的內(nèi)容
如果在摸一個具體模板中不希望使用渲染模板,可以在頁首添加{__nocontent__}
2、不開啟自動模板渲染可以在每一個具體頁面的頁首添加
<layout name=’layout’/>
3.使用技巧
在渲染模板文件中也可以使用其他模板文件的內(nèi)容
<include file=’public:header’/>
<body>
<p>這里是渲染頁面?。。?lt;/p>
{__content__}
</body>
</html>
模板的繼承
控制器的模塊和操作
空模塊和空操作
1、空操作
function _empty($name){
$this->show(“$name 不存在 <a href=’__app__/index/index’>返回首頁</a>”);
}
2.空模塊
class emptyaction extends action{
function index(){
$city=m(‘city’);
$arr=$city->select();
$this->assign(‘list’,$arr);
$name=module_name;
$this->display(“city:$name”);
}
}
前置操作和后置操作
1、前置操作: _before_操作名
2、后置操作: _after_操作名
url
url規(guī)則
1、默認(rèn)是區(qū)分大小寫的
2、如果我們不想?yún)^(qū)分大小寫可以改配置文件
‘url_case_insensitive’=>true,//url不區(qū)分大小寫
3、如果模塊名為 usergroupaction
那么url找模塊就必要要寫成
http://localhost/thinkphp4/index.php/user_group/index
4、如果’url_case_insensitive’=>false
那么url也可以寫為
http://localhost/thinkphp4/index.php/usergroup/index
url偽靜態(tài)
‘url_html_suffix’=>’html|shtml|xml’,//限制偽靜態(tài)的后綴
url路由
1、啟動路由
要在配置文件中開啟路由支持
2、使用路由
1.規(guī)則表達(dá)式配置路由
‘my’=>’index/index’,//靜態(tài)地址路由
‘:id/:num’=>’index/index’,//動態(tài)地址路由
‘year/:year/:month/:date’=>’index/index’,//動態(tài)和靜態(tài)混合地址路由
‘year/:yeard/:monthd/:dated’=>’index/index’,//動態(tài)和靜態(tài)混合地址路由
加上 d代表類型只能是數(shù)字
‘my/:id$’=>’index/index’,// 加上$說明地址中只能是 my/1000 后面不能有其他內(nèi)容了
2.正則表達(dá)式配置路由
‘/^year/(d{4})/(d{2})/(d{2})/’=>’index/index?year=:1&month=:2&date=:3′
3、注意事項(xiàng):
1.越復(fù)雜的路由越往前面放
‘url_route_rules’=>array(
‘my/:year/:month:/:day’=>’index/day’,
‘my/:idd’=>’index/index’,
‘my/:name’=>’index/index’,
)
2.可以使用$作為完全匹配的路由規(guī)則
‘url_route_rules’=>array(
‘my/:idd$’=>’index/index’,
‘my/:name$’=>’index/index’,
‘my/:year/:month:/:day$’=>’index/day’,
),
3.用正則匹配的方式
‘url_route_rules’=>array(
‘/^my/(d+)$/’=>’index/index?id=:1′,
‘/^my/(w+)$/’=>’index/index?name=:1′,
‘/^my/(d{4})/(d{2})/(d{2})$/’=>’index/day?year=:1&month=:2&day=:3′,
),
url重寫
url生成
分組、頁面跳轉(zhuǎn)與ajax
多應(yīng)用配置技巧
使用分組
頁面跳轉(zhuǎn)
$this->success(‘查詢成功’,u(‘user/test’));
$this->redirect(‘user/test’,”,5,’頁面正在跳’);
ajax技巧
大c方法 獲取配置文件中數(shù)組名稱和值
echo c(‘db_user’);
大f方法 文件處理
寫:f(‘文件名’,’數(shù)組’,’目錄’);
讀:f(‘文件名’,”,’目錄’);
大u方法 url處理
在php里
u(‘方法名’)
在模板中
當(dāng)前函數(shù){:u(‘方法名’)}
其他函數(shù){:u(‘函數(shù)/方法名’)}
*文件引入
<css file=’__public__/css/base.css’ />
<js file=’__public__/js/base.js’ />
*表單處理
方法1 $this->_post(”);
獲取提交表單,會使用函數(shù)htmlspecialchars()過濾
$username=$this->_post(‘username’);
方法2 i(‘username’); [3.1.3新功能]
大i,自動判斷post和get
$username=i(‘username’);
echo i(‘username’,’不存在值時的默認(rèn)值’,’使用函數(shù)’);
查看是否有數(shù)據(jù)提交:
print_r(i(‘post.’));
禁止表單處理函數(shù)的訪問,提升安全性
方法1
if(!$this->ispost()) _404(‘頁面不存在’,u(‘index’));
echo ‘正常提交';
方法2 (推薦) halt 頁面可以定制錯誤頁面:
if(!is_post) halt(‘頁面不存在’);
echo ‘正常提交';
制定方法:在conf/config.php 添加:’tmpl_exception_file’=>’./public/tpl/error.html’,
文件接受錯誤內(nèi)容:./public/tpl/error.html,只能寫原生php,支持常量,如__app__
<?php echo $e['message']; ?>
//返回插入的id值,數(shù)據(jù)庫從1開始
if(m(‘user’)->data($data)->add()){
$this->success(‘添加成功’,’index’);
}else{
$this->error(‘添加失敗’);
}
*輸出到模板
1,數(shù)據(jù)準(zhǔn)備
方法1
$this->assign(‘變量名’,’變量值’)
方法2
$this->變量名=’變量值';
方法3(新版本,縮短了代碼)
$this->assign(‘變量名’,’變量值’)->display();
方法4(一行搞定)
$this->assign(‘data’,m(‘user’)->select())->display();
2,模板輸出
方法1(.語法會判斷是對象還是數(shù)組,配置參數(shù):tmp_var_identify=>’array’, 這樣就會認(rèn)為是數(shù)組,以提高速度)
<foreach name=’data’ item=’v’>
{$v.username}—{$v.password}—{$v.time|date=’y-m-d h:i:s,###’}
</foreach>
使用函數(shù) {:phpinfo()}
方法2
volist
*分組應(yīng)用(應(yīng)用組)前后臺只用一個入口文件
idnex.php (默認(rèn))
<?php
define(‘a(chǎn)pp_name’,’app’);
define(‘a(chǎn)pp_path’,’./app/’);
define(‘a(chǎn)pp_debug’,’true’);
require ‘./thinkphp/thinkphp.php';
?>
app/conf/config.php
<?php
return array(
//開啟分組
‘a(chǎn)pp_group_list’=>’index,admin’,
‘default_group’=>’index’,
‘tmpl_file_depr’=>’_’, //默認(rèn)模板分隔符為_而非文件夾形式
);
自定義控制器
1,在action文件夾刪除原來的默認(rèn)控制器
2,建立兩個文件夾,分別為前臺和后臺如index admin
訪問: 前臺host/index.php/index 后臺host/index.php/admin
自定義配置文件(互相不能訪問,但公共配置可以互相訪問)
1,在host/app/conf/建立兩個文件夾,分別為前臺和后臺如index admin
配置: 前臺host/app/conf/index/config.php
后臺host/app/conf/admin/config.php
自定義函數(shù)
1,在host/app/common/建立兩個文件夾,分別為前臺和后臺如index admin
配置: 前臺host/app/common/index/function.php
后臺host/app/common/admin/function.php
注意:分組應(yīng)用不支持{:u(“form_save”)}直接使用,即當(dāng)前控制器當(dāng)前方法,這樣寫:
{:u(“/index/index/form_save”)}
*分組應(yīng)用[完畢]
**獨(dú)立分組配置
開啟:
1,先配置應(yīng)用分組
2,添加參數(shù)
‘a(chǎn)pp_group_mode’=>1, //開啟獨(dú)立分組
‘a(chǎn)pp_group_path’=>’modules’, //默認(rèn)分組路徑,默認(rèn):modules
常用路徑變量:__root__.’/’.app_name.’/’.c(‘a(chǎn)pp_group_path’).’/’.group_name.’/’
新的目錄結(jié)構(gòu):
thinkphp //系統(tǒng)目錄
app //項(xiàng)目目錄
public //靜態(tài)文件目錄
index.php //所有項(xiàng)目的入口文件
app:
modules //項(xiàng)目模塊 ==>admin/action,tpl,model,conf,common; index/action,tpl,model,conf,common;
common //公共common
conf //公共conf
lib //公共lib
tpl //公共tpl
runtimes //運(yùn)行時
public:
css js img
**jquery 異步提交
$(‘#send-btn’).click(
function(){
var username=$(‘input[username=username]‘);
var password=$(‘input[password=password]‘);
if(username.val()==”){
alert(‘用戶名不能為空’);
username.focus();
//讓用戶名獲取焦點(diǎn)
return;
}
if(password.val()==”){
alert(‘密碼不能為空’);
password.focus();
//讓用戶名獲取焦點(diǎn)
return;
}
//開始異步傳輸(要在模板內(nèi)才可以解析php,不可以在單獨(dú)js文件操作)
var sendurl='{:u(“index/index/form_save”,”,”)}';
$.post(sendurl,{username:username.val(),password:password.val()},function(data){},’json’);
}
);
**php異步表單處理:
public function form_save(){
//var_dump($this->isajax()); //判斷是否有數(shù)據(jù)提交
//var_dump(is_ajax); //[新版本]判斷是否有數(shù)據(jù)提交
if(!$this->isajax()) halt (‘頁面不存在’);
$data=array(
‘username’=>$this->_post(‘username’),
‘password’=>$this->_post(‘password’),
‘time’=>time()
);
//p($data);
}
11課開始沒記錄
*引入模塊
1,驗(yàn)證碼模塊,核心包里沒有,需要手動添加,放在thinkphp/extend/library/org/util/image.class.php
import(‘org.util.image’);
image::buildimageverify(4,5,’png’);
訪問:該方法地址即可
換驗(yàn)證碼,使用一個js隨機(jī)數(shù)函數(shù):math.random();
注意:如果出現(xiàn)偽靜態(tài)后綴名,需要在{:u(‘a(chǎn)dmin/login/index’),”,”} 增加2個空參數(shù)
混合模式區(qū)分大小寫
驗(yàn)證碼后臺驗(yàn)證
if(md5($_post[code])!= session('verify')){ $this->error('驗(yàn)證碼錯誤');}
if(md5($this->_post[code])!= session('verify')){ $this->error('驗(yàn)證碼錯誤');}
if(i('code','','md5')!= session('verify')){ $this->error('驗(yàn)證碼錯誤');}
2,分頁類
import('org.util.page'); //導(dǎo)入類
$max=m('user')->count(); //查詢總條數(shù)
$page=new page($max,5,'','index/index/index/p/');
//實(shí)例化對象,參數(shù):1總條數(shù),2每頁條數(shù),3分頁跳轉(zhuǎn)的參數(shù),4設(shè)置url路徑,在分組時有用,因?yàn)樯賯€應(yīng)用組名
$limit=$page->firstrow.','.$page->listrows; //獲取第一頁和總頁數(shù)并設(shè)置為limit值
$m=m('user')->order('time desc')->limit($limit)->select(); //查出第一頁的數(shù)據(jù)
$this->user=$m; //把數(shù)據(jù)分配給前臺模板,用于foreach遍歷
$this->page=$page->show(); //把分頁內(nèi)容分配給前臺模板
模板里分頁數(shù)據(jù)調(diào)用:{$page}
**session 保存到數(shù)據(jù)庫
1, 配置文件添加: 'session_type'=>'db',
更多信息請查看IT技術(shù)專欄