## 請求信息
namespace app\index\controller;
use \think\Request;
class Index
{
//聲明請求對象
public function hello() {
//使用Request類
$request = Request::instance();
//建議使用助手函數
$request = request();
return $request->param();
}
//需要時,建議直接注入請求對象
public function hello1(Request $request) {
//獲取URL信息
echo '當前域名: ' . $request->domain() . '<br/>';
echo '當前入口文件: ' . $request->baseFile() . '<br/>';
echo '當前URL地址 不含域名: ' . $request->url() . '<br/>';
echo '包含域名的完整URL地址: ' . $request->url(true) . '<br/>';
echo '當前URL地址 不含QUERY_STRING: ' . $request->baseUrl() . '<br/>';
echo 'URL訪問的ROOT地址:' . $request->root() . '<br/>';
echo 'URL訪問的ROOT地址含域名: ' . $request->root(true) . '<br/>';
echo 'URL地址中的PATH_INFO信息: ' . $request->pathinfo() . '<br/>';
echo 'URL地址中的PATH_INFO信息 不含后綴: ' . $request->path() . '<br/>';
echo 'URL地址中的后綴信息: ' . $request->ext() . '<br/>';
//設置和獲取模塊/控制器/操作名稱
echo "當前模塊名稱是" . $request->module();
echo "當前控制器名稱是" . $request->controller();
echo "當前操作名稱是" . $request->action();
$request->module('module_name'); //設置模塊名稱
$request->controller('controller_name'); //設置控制器名稱
$request->action('action_name'); //設置操作名稱
//獲取請求參數
echo '請求方法:' . $request->method() . '<br/>';
echo '資源類型:' . $request->type() . '<br/>';
echo '訪問地址:' . $request->ip() . '<br/>';
echo '是否AJax請求:' . var_export($request->isAjax(), true) . '<br/>';
echo '請求參數:';
dump($request->param());
echo '請求參數:僅包含name';
dump($request->only(['name']));
echo '請求參數:排除name';
dump($request->except(['name']));
//獲取路由和調度信息
echo '路由信息:';
dump($request->route());
echo '調度信息:';
dump($request->dispatch());
//設置請求信息
$request->root('index.php');
$request->pathinfo('index/index/hello');
return '';
}
}
## 輸入變量
public function hello(Request $request) {
//變量檢測: has('變量名','變量類型')
$request->has('id', 'get');
input('?get.id'); //使用助手函數
//獲取變量: 變量類型方法('變量名/變量修飾符','默認值','過濾方法')
$request->param('id');
input('param.id'); //使用助手函數
//變量過濾:過濾方式包括函數、方法過濾,以及PHP內置的Types of filters
//采用Filter ID 進行過濾的話,如果不符合過濾要求的話會返回false,
//因此你需要配合默認值來確保最終的值符合你的規范。
$request->filter(['htmlspecialchars']); //設置全局過濾
$request->param('username', '', 'strip_tags,strtolower'); //獲取變量時指定過濾方法
$request->post('email', '', FILTER_VALIDATE_EMAIL); //PHP內置提供的Filter ID過濾
$request->post('email', '', 'email'); //框架對Filter ID做了轉換
$request->get('name', '', 'strtolower', true); //配合全局過濾方法
//獲取部分變量
$request->only('id,name', 'get');
$request->only(['id','name'], 'post');
//排除部分變量
$request->except('id,name', 'get');
$request->except(['id','name'], 'post');
//變量修飾符
//引用方法: 變量類型方法('變量名/修飾符')
$request->get('id/d');
input('param.id/d'); //使用助手函數
return $request->param();
}
> 變量類型:
> |方法|描述|
> |---|---|
> |param |獲取當前請求的變量|
> |get |獲取 $_GET 變量|
> |post |獲取 $_POST 變量|
> |put |獲取 PUT 變量|
> |delete |獲取 DELETE 變量|
> |session |獲取 $_SESSION 變量|
> |cookie |獲取 $_COOKIE 變量|
> |request |獲取 $_REQUEST 變量|
> |server |獲取 $_SERVER 變量|
> |env |獲取 $_ENV 變量|
> |route |獲取 路由(包括PATHINFO) 變量|
> |file |獲取 $_FILES 變量|
> 變量修飾符:
> |修飾符|作用|
> |---|---|
> |s |強制轉換為字符串類型,TP5默認|
> |d |強制轉換為整型類型|
> |b |強制轉換為布爾類型|
> |a |強制轉換為數組類型|
> |f |強制轉換為浮點類型|
## 更改變量
public function hello(Request $request) {
//修改輸入變量
//盡量避免直接修改$_GET 或者 $_POST數據
//不能直接修改param變量
$request->get(['id'=>10]);
$request->post(['name'=>'thinkphp']);
$request->param(['name'=>'thinkphp']); //此操作無效
return '';
}
## 請求類型
public function hello(Request $request) {
if ($request->isGet()) echo "當前為 GET 請求";
if ($request->isPost()) echo "當前為 POST 請求";
if ($request->isPut()) echo "當前為 PUT 請求";
if ($request->isDelete()) echo "當前為 DELETE 請求";
if ($request->isAjax()) echo "當前為 Ajax 請求";
if ($request->isPjax()) echo "當前為 Pjax 請求";
if ($request->isMobile()) echo "當前為手機訪問";
if ($request->isHead()) echo "當前為 HEAD 請求";
if ($request->isPatch()) echo "當前為 PATCH 請求";
if ($request->isOptions()) echo "當前為 OPTIONS 請求";
if ($request->isCli()) echo "當前為 cli";
if ($request->isCgi()) echo "當前為 cgi";
return '';
}
## 請求偽裝
//將POST請求偽裝成PUT請求
//合法的請求類型包括GET、POST、PUT和DELETE等
<form method="post" action="">
<input type="text" name="name" value="Hello">
<input type="hidden" name="_method" value="PUT" >
<input type="submit" value="提交">
</form>
//修改配置文件
//表單請求類型偽裝變量
'var_method' => '_m', //默認值為'_mothod',如上
## 方法參數綁定
方法參數綁定是把URL地址(或者路由地址)中的變量作為操作方法的參數直接傳入。
控制器方法如下:
public function read($id)
{
return 'id='.$id;
}
public function archive($year='2016',$month='01')
{
return 'year='.$year.'&month='.$month;
}
按名稱綁定,當請求URL地址是:
//URL中參數名和方法參數名必須一致,順序可以調整
http://serverName/index.php/index/blog/read/id/5
http://serverName/index.php/index/blog/archive/month/06/year/2016
//方法中參數如果沒有默認值,URL訪問時必須帶此參數,否則會報錯
http://serverName/index.php/index/blog/read //頁面會返回錯誤
輸出結果為:
id=5
year=2016&month=06
按順序綁定, 合理規劃URL參數的順序綁定可以簡化URL地址。
修改配置參數:
// URL參數方式改成順序解析
'url_param_type' => 1,
當請求URL地址是:
//URL參數的順序要與方法中參數順序一致
http://serverName/index.php/index/blog/archive/2016/06
http://serverName/index.php/index/blog/archive/06/2016
輸出結果為:
year=2016&month=06
year=06&month=2016
> ## 注意
> 按順序綁定參數的話,操作方法的參數只能使用URL pathinfo變量,而不能使用get或者post變量。
> 參數綁定有一個特例,如果你的操作方法中定義有Request對象作為參數的話,無論參數位置在哪里,都會自動注入,而不需要進行參數綁定。
## 注入請求對象
控制器的操作方法中如果需要調用請求對象Request的話,可以在方法中定義Request類型的參數,并且參數順序無關。
如果繼承了系統的Controller類的話,也可以直接調用request屬性。
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function hello()
{
return 'Hello,'.$this->request->param('name');
}
}
## 獲取HTTP頭信息
HTTP請求頭信息的名稱不區分大小寫。
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function hello()
{
$info = $this->request->header();
$agent = $this->request->header('user-agent');
echo $info['accept'];
echo $info['accept-encoding'];
echo $info['user-agent'];
echo $agent;
}
}
## 方法注入
// 通過hook方法注入動態方法
Request::hook('user','getUserInfo');
//getUserInfo函數定義如下
function getUserInfo(Request $request, $userId)
{
// 根據$userId獲取用戶信息
return $info;
}
//接下來,我們可以直接在控制器中使用
public function index()
{
$info = Request::instance()->getUserInfo($userId);
}
## 屬性注入
// 動態綁定屬性
Request::instance()->bind('user',new User);
// 或者使用
Request::instance()->user = new User;
//獲取綁定的屬性使用下面的方式:
Request::instance()->user;
$this->request->user; //如果繼承了系統的Controller類的話
request()->user;
## 偽靜態
URL偽靜態通常是為了滿足更好的SEO效果。
修改配置參數
// 打開偽靜態后綴訪問
'url_html_suffix' => ture,
// 后綴類型
'url_html_suffix' => 'shtml|html', //默認值為'html'
按照上面的配置,以下訪問是有效:
http://serverName/index/blog/3
http://serverName/index/blog/3.html
http://serverName/index/blog/3.shtml
按歸上面的配置,以下訪問是無效的:
http://serverName/index/blog/3.xml
http://serverName/index/blog/3.pdf
在控制器的操作方法中獲取當前訪問的偽靜態后綴:
$ext = Request::instance()->ext();