養(yǎng)成了習(xí)慣,記錄下學(xué)習(xí)TP5的筆記
這可能是最亂的筆記,也可能是只有自己能看懂的筆記,
沒有捷徑,只有動手,遇到問題首先查手冊,是Tp5封裝的基本都能解決,畢竟是小白
不過tp5和tp3截然不同,也是耗費(fèi)了我不少腦細(xì)胞的,內(nèi)存有限,所以線寫入磁盤吧哈哈
下面開始我的表演:
入門安裝
安裝完畢后首先開啟config.php中的debug方便調(diào)試
隱藏index.php文件 修改.htaccess文件,需要apache開啟rewrite模塊,配置文件中所有 AllowOverride 設(shè)置為 All
參數(shù)獲取
訪問www.test.com/admin/index/index/aa/11/bb/22/cc/33/name/lisa
在方法中print_r($this->request->param()); //打印路由中所有參數(shù)
數(shù)據(jù)表前綴的設(shè)置
配置文件中prefix = 'tp_'];
數(shù)據(jù)庫的簡單操作、
$data = Db::name('user') -> find(); //可用select();
$this->assign('data',$data); //前臺使用數(shù)據(jù)舉例 {$data} {$data['name']}
return $this->fetch(); //return view();使用助手函數(shù)
路由和URL
訪問ADMIN/INDEX/INDEX 轉(zhuǎn)化
控制器轉(zhuǎn)化為Index 首頁字母為大寫 如果是HelloWorld控制器就要用hello_world訪問
方法名和模塊名都轉(zhuǎn)化為小寫
配置文件
'URL_convert' => true;轉(zhuǎn)化參數(shù)配置,關(guān)了以后就不轉(zhuǎn)化了,
TP5中 index?m=home&c=index&a=index 訪問形式不在支持
model命名
model名字需要和表名相同
路由轉(zhuǎn)化
修改route.php文件
url偽靜態(tài)后綴設(shè)置: 'url_html_suffix' => 'html';
'hello/[:name]' => ['index/index/hello',['method'=>'get','ext'=>'html']];
接下來
1、訪問www.test.com/hello/zhangsan.html(擴(kuò)展為html)的時候,
即可訪問到index/index/hello,而且可以通過get方式獲取到參數(shù)name值zhangsan;
2、訪問www.test.com/hello.html(擴(kuò)展為html)的時候,
即可訪問到index/index/hello,;即name參數(shù)可帶可不帶
修改route.php文件
'hello/:name' => ['index/index/hello',['method'=>'get','ext'=>'html']];
注意name不帶[]這時候方式時就必須帶參數(shù)了用上邊第二種方法就不行了
多個參數(shù)的情況:
'today/:year/:mouth' => ['index/index/today',['mouth' => 'get'],['year' => '\d{4}','mouth'=>'\d{2}']];
在today方法中有year mouth、兩個參數(shù) 用www.test.com/today/2017/10.html
url生成:
url::build('admin/hello','a=1&b=2');
助手函數(shù):
url('admin/hello','a=1&b=2');
url('admin/hello',['a'=>1,'b'=>2]);
url('admin/HelloWorld/index'); 會自動切換成hello_world
請求響應(yīng)
Request類
繼承Controller類就已經(jīng)實(shí)例化過此類,不繼承可用Request::instance();實(shí)例化
$this -> request ->url(); 同 $request -> url(); 獲取url不含域名;
獲取參數(shù)
$request -> param(); 獲取所有參數(shù)
$request -> param('name'); 獲取name值
也可用input助手函數(shù)
input();獲取所有 ; input('name');獲取name;
$this -> request ->bind('username','張三'); 動態(tài)綁定參數(shù)
$this -> request -> username; 獲取綁定的name值
$request ->param('en_name','jake','strtolower');把url傳過來的值轉(zhuǎn)化為小寫,不傳則為默認(rèn)值jake
參數(shù)
$request -> get(); get('name');
$request -> post(); post('name');
$request -> cookie('name');
$request -> file('image');
$request ->method();
$request ->ip()
$request ->isAjax()?'是':'否';
$request ->domain();
$request ->baseFile();入口文件
$request ->url(true);設(shè)為true含域名完整url
$request ->query();url的參數(shù)信息
$request ->baseUrl();不含域名不含參數(shù)
$request ->pathinfo();返回url中pathinfo信息
$request ->ext();返回后綴
$request ->module();模塊名
$request ->controller();控制器名
$request ->action();方法名