> ## 獲得請求對象
```
// 方?法一:實例化對象
1.use think\Request;
2.$request = Request::instance();
// 方法?二: 助手函數
$request = request();
// 方法三:依賴注入
1.use think\Request;
2.在方法的形參表添加Request $request
```
>## 獲取URL信息
```
$request = Request::instance();
// 獲取當前域名
echo 'domain: ' . $request->domain() . '<br/>'; // http://tp5.com
// 獲取當前入口文件
echo 'file: ' . $request->baseFile() . '<br/>'; // /index.php
// 獲取當前URL地址 不含域名
echo 'url: ' . $request->url() . '<br/>'; // /index/index/hello.html?name=thinkphp
// 獲取包含域名的完整URL地址
echo 'url with domain: ' . $request->url(true) . '<br/>'; // http://tp5.com/index/index/hello.html?name=thinkphp
// 獲取當前URL地址 不含QUERY_STRING
echo 'url without query: ' . $request->baseUrl() . '<br/>'; // /index/index/hello.html
// 獲取URL訪問的ROOT地址
echo 'root:' . $request->root() . '<br/>'; //
// 獲取URL訪問的ROOT地址
echo 'root with domain: ' . $request->root(true) . '<br/>'; // http://tp5.com
// 獲取URL地址中的PATH_INFO信息
echo 'pathinfo: ' . $request->pathinfo() . '<br/>'; // index/index/hello.html
// 獲取URL地址中的PATH_INFO信息 不含后綴
echo 'pathinfo: ' . $request->path() . '<br/>'; // index/index/hello
// 獲取URL地址中的后綴信息
echo 'ext: ' . $request->ext() . '<br/>'; // html
```
>## 獲取或設置 模塊/控制器/操作
```
// 獲取
$request = Request::instance();
echo "當前模塊名稱是" . $request->module();
echo "當前控制器名稱是" . $request->controller();
echo "當前操作名稱是" . $request->action();
// 設置
Request::instance()->module('module_name')
```
>## 獲取請求參數
```
$request = Request::instance();
echo '請求方法:' . $request->method() . '<br/>'; // GET
echo '資源類型:' . $request->type() . '<br/>'; // html
echo '訪問ip地址:' . $request->ip() . '<br/>'; // 127.0.0.1
echo '是否AJax請求:' . var_export($request->isAjax(), true) . '<br/>'; // false
echo '請求參數:';
dump($request->param()); // array (size=2) 'test' => string 'ddd' (length=3) 'name' => string 'thinkphp' (length=8)
echo '請求參數:僅包含name';
dump($request->only(['name'])); // array (size=1) 'name' => string 'thinkphp' (length=8)
echo '請求參數:排除name';
dump($request->except(['name'])); // array (size=1) 'test' => string 'ddd' (length=3)
```
>## 獲取路由和調度信息
```
$request = Request::instance();
dump($request->route()); // 路由信息
dump($request->dispatch()); // 調度信息
// 假設路?由定義
return [
'hello/:name' =>['index/hello',[],['name'=>'\w+']],
];
// 假設訪問的url為
http://serverName/hello/thinkphp
// 結果
路由信息:
array (size=4)
'rule' => string 'hello/:name' (length=11)
'route' => string 'index/hello' (length=11)
'pattern' =>
array (size=1)
'name' => string '\w+' (length=3)
'option' =>
array (size=0)
empty
調度信息:
array (size=2)
'type' => string 'module' (length=6)
'module' =>
array (size=3)
0 => null
1 => string 'index' (length=5)
2 => string 'hello' (length=5)
```
>## 設置請求信息
```
$request = Request::instance();
$request->root('index.php');
$request->pathinfo('index/index/hello');
```
- 運行環境需求
- tp5目錄結構
- 命令行生成代碼
- 路由
- 請求
- 獲取請求信息
- 超全局變量獲取
- 更改請求變量的值
- 判斷是否為某種請求類型
- 偽裝表單請求類型
- HTTP頭部信息
- 偽靜態
- 向請求對象中注入自定義的屬性和方法
- 簡單的傳參可以使用參數綁定
- 依賴注入(將對象注入方法作為參數)
- 將請求的數據進行緩存
- 控制器
- 一個控制器代碼示例
- 空控制器
- 資源控制器
- 模型
- 一個模型代碼示例
- 模型的四種調用方法
- 控制器中調用模型添加數據
- 控制器中調用模型更新數據
- 控制器中調用模型刪除數據
- 控制器中調用模型查詢數據
- 模型中使用聚合函數
- 獲取器
- 修改器
- 自動寫入時間戳
- 只讀字段
- 軟刪除
- 自動類型轉換
- 數據自動完成
- 查詢范圍
- 數組方式訪問和轉換為數組
- json序列化
- 模型的事件
- 關聯模型
- 一對一關聯
- 一對多關聯
- 遠程一對多(跨表關聯)
- 多對多關聯
- 多態關聯
- 關聯預載入N+1次查詢變2次
- 延遲預載入
- 關聯統計
- 視圖與模板
- 模板引擎配置
- 分配數據到模板
- 輸出替換
- 模板中輸出變量
- 模板中輸出系統變量(配置常量超全局)
- 模板中輸出請求信息
- 模板中使用php函數
- 輸出到模板中的變量指定默認值
- 模板中進行運算
- 原樣輸出代碼不解析
- 模版中注釋
- 模板布局與繼承
- 文件包含
- 內置標簽
- 循環輸出標簽
- 比較標簽
- 條件判斷標簽
- 引入資源文件(js-css)
- 使用原生php
- 在模板中定義變量和常量
- 助手函數
- 常用功能
- 表單驗證
- 驗證器的定義
- 自定義驗證規則
- 速查表
- 系統默認根命名空間
- 系統路徑常量
- 請求變量
- URL請求和信息方法