參數綁定
方法(fa)參(can)數綁定是把URL地(di)址(或(huo)者路由地(di)址)中(zhong)的(de)變量作為操作方法(fa)的(de)參(can)數直(zhi)接傳入。
操作方法參數綁定
按名稱綁定
參數綁定方式默認是按照變量名進行綁定,例如,我們給Blog控制器定義了兩個操作方法read和archive方法,由于read操作需要指定一個id參數,archive方法需要指定年份(year)和月份(month)兩個(ge)參數,那么(me)我們可(ke)以如(ru)下定(ding)義:
namespace appindexController;
class Blog
{
public function read($id)
{
return 'id='.$id;
}
public function archive($year='2016',$month='01')
{
return 'year='.$year.'&month='.$month;
}
}
注(zhu)意這里(li)的(de)操作方(fang)法并沒有具體的(de)業務(wu)邏(luo)輯,只是簡單的(de)示范。
URL的訪問地址分(fen)別是:
http://serverName/index.php/index/blog/read/id/5
http://serverName/index.php/index/blog/archive/year/2016/month/06
兩個URL地址中的id參數和year和month參數會自動和read操作方法以及archive操作方法的同名參數綁定。
變量名綁定不一定由(you)訪(fang)問URL決定,路由(you)地址(zhi)也能起(qi)到相(xiang)同的(de)作用
輸(shu)出的結果依次是:
id=5
year=2016&month=06
按照變量名進行(xing)參數(shu)(shu)綁定(ding)的參數(shu)(shu)必須和URL中傳入的變量名稱一致(zhi),但是參數(shu)(shu)順序不需要(yao)一致(zhi)。也就是說
http://serverName/index.php/index/blog/archive/month/06/year/2016
和(he)上面的(de)訪問結果是一(yi)(yi)致的(de),URL中(zhong)的(de)參(can)(can)數順序(xu)和(he)操作(zuo)方法中(zhong)的(de)參(can)(can)數順序(xu)都可以(yi)隨(sui)意調(diao)整,關鍵是確保參(can)(can)數名稱(cheng)一(yi)(yi)致即可。
如果用(yong)戶訪(fang)問(wen)的URL地址是(至于為什(shen)么會這么訪(fang)問(wen)暫且不提):
//serverName/index.php/index/blog/read/
那么會拋出下面的異常提示: 參數錯誤:id
報錯(cuo)的(de)(de)原因很簡單,因為(wei)在執(zhi)行read操(cao)作方法(fa)(fa)的(de)(de)時候(hou),id參數(shu)是必須傳入(ru)參數(shu)的(de)(de),但是方法(fa)(fa)無法(fa)(fa)從URL地(di)址中獲取正確的(de)(de)id參數(shu)信息。由于我們不(bu)能相(xiang)信用戶的(de)(de)任何輸入(ru),因此建議(yi)你給(gei)read方法(fa)(fa)的(de)(de)id參數(shu)添加默認(ren)值,例如:
public function read($id=0)
{
return 'id='.$id;
}
這樣,當我們訪問 //serverName/index.php/index/blog/read/ 的時(shi)候 就會輸出
id=0
始(shi)終給操作方法的參數定義默認值是一個(ge)避免(mian)報錯的好(hao)辦法
按順序綁定
還可以支持按照URL的(de)(de)參(can)數順(shun)序進行綁定(ding)的(de)(de)方(fang)式,合理規劃URL參(can)數的(de)(de)順(shun)序綁定(ding)對簡(jian)化URL地址(zhi)可以起到一定(ding)的(de)(de)幫助。
還是(shi)上面的例子,控制(zhi)器不變,還是(shi)使用(yong):
namespace appindexController;
class Blog
{
public function read($id)
{
return 'id='.$id;
}
public function archive($year='2016',$month='01')
{
return 'year='.$year.'&month='.$month;
}
}
我(wo)們在配(pei)置文件中添加配(pei)置參數如下:
// URL參數(shu)方式改成順序解析
'url_param_type' => 1,
接下來,訪(fang)問下面的URL地(di)址:
//serverName/index.php/index/blog/read/5
//serverName/index.php/index/blog/archive/2016/06
輸出的結果依次是:
id=5
year=2016&month=06
按參(can)數順(shun)序綁定的(de)話(hua),參(can)數的(de)順(shun)序不能隨意調整,如果訪問:
http://serverName/index.php/index/blog/archive/06/2016
最后的輸(shu)出結果則變成:
id=5
year=06&month=2016
注意
按順序綁定參數的話,操作(zuo)方法的參數只能使用(yong)URL pathinfo變量,而不能使用(yong)get或者post變量。
參數綁定有一個特例,如果你的操作方法中定義有Request對象作為參(can)數的話,無(wu)論參(can)數位置在哪里,都會自動注入,而不需要進(jin)行(xing)參(can)數綁定。
架構方法參數綁定(V5.0.1)
可以(yi)對架構函(han)數(shu)進(jin)行參數(shu)綁(bang)定(ding),當前請求的路由(you)變(bian)量可以(yi)自動綁(bang)定(ding)到架構函(han)數(shu)的參數(shu),例如:
namespace appindexController;
class Blog
{
protected $name;
public function __construct($name = null)
{
$this->name = $name;
}
}
如果訪問
//localhost/index/index/index/name/thinkphp
當前請求的(de)(de)路由變(bian)量name的(de)(de)值(zhi)thinkphp會自(zi)動傳入架構方法(fa)的(de)(de)name變(bian)量。
