TP5 Request請求類
獲取請求信息
- 獲取請求信息
- 使用
\think\Request
類\thinkphp\library\think\Request.php
$request = \think\Request::instance();
- 使用助手函數
\thinkphp\helper.php
$request = request();
- 使用
獲取param變量
- 獲取param變量
- param變量是框架提供的用於自動識別
GET POST PUT
請求的一種變量獲取方式 - param方法會把當前請求類型的參數和PATH_INFO變量以及GET請求合並
- param變量是框架提供的用於自動識別
# Request類的實現
// 獲取當前請求的name變量
Request::instance()->param('name');
// 獲取當前請求的所有變量(經過過濾)
Request::instance()->param();
// 獲取當前請求的所有變量(原始數據)
Request::instance()->param(false);
// 獲取當前請求的所有變量(包含上傳文件)
Request::instance()->param(true);
# 使用助手函數實現
input('param.name');
input('param.');
# 或者
input('name');
input('');
# 單獨獲取get變量或者post變量
Request::instance()->get();
Request::instance()->post();
input('get.');
input('post.');
變量過濾
-
設置全局過濾規則
default_filter => 'htmlspecialchars'
-
使用Request對象進行全局變量的獲取過濾
- 過濾方式包括函數、方法過濾
- PHP內置的Types of fillers
Request::instance()->filter('htmlspecialchars');
Request::instance()->filter(['strip_tags','htmlspecialchars']);
-
在獲取變量的時候添加過濾方法
- 獲取get變量,並用htmlspecialchars函數過濾
Request::instance()->get('name','','htmlspecialchars');
- 獲取param變量,並用strip_tags函數過濾
Request::instance()->param('username','','strip_tags');
- 獲取post變量,並用org\Filter類的safeHtml方法過濾
Request::instance()->post('name','','org::Filter');
- 獲取get變量,並用htmlspecialchars函數過濾
-
路由變量與get變量
http://www.tpshop.com/home/test/index/id/100?page=10
- param方法: 能夠獲取所有參數(id, page)
- get方法: 只能獲取?后面的請求字符串的參數(page)
- route方法: 只能獲取到?前面的路由中的參數(id)
參數綁定
- 概念
- 參數綁定方式默認是按照變量名進行綁定
- 例如,給Blog控制器定義了兩個操作方法
read
和archive
方法read
操作需要指定一個id
參數archive
方法需要指定年份year
和月份month
兩個參數
namespace app\index\Controller;
class Blog
{
public function read($id)
{
return 'id='.$id;
}
public function archive($year='2021',$month='03')
{
return 'year='.$year.'&month='.$month;
}
}
- URL的訪問地址分別是
http://serverName/index.php/index/blog/read/id/5
http://serverName/index.php/index/blog/archive/year/2021/month/08
依賴注入
-
概念
- 要在一個類A中使用另一個依賴類B時
- 不直接在類A中實例化類B
- 而是先實例化類B后再以參數的形式傳入類A
-
操作方法注入
- 控制器的操作方法中如果需要調用請求對象
Request
- 可以在方法中定義
Request
類型的參數,並且與參數順序無關 - 訪問URL地址的時候,無需傳入
request
參數 - 系統會自動注入當前的
Request
對象實例到該參數
- 控制器的操作方法中如果需要調用請求對象
namespace app\index\controller;
use think\Request;
class Index
{
public function hello(Request $request)
{
return 'Hello,'.$request->param('name').'!';
}
}
- 方法繼承注入
- 如果繼承了系統的
Controller
類的話,也可以直接調用request
屬性
- 如果繼承了系統的
namespace app\index\controller;
use think\Request;
class Index extends Controller
{
public function hello()
{
return 'Hello,'.$this->request->param('name').'!';
}
}
- 依賴注入原理分析
# 實例化普通寫法
<?php
class User{
public function getName()
{
return 'user-name';
}
}
class Goods{
public function detail()
{
// 實例化User類
$user= new User();
$username= $user->getName();
return $username;
}
}
$goods= new Goods();
echo $goods->detail();
?>
# 依賴注入寫法
<?php
class User{
public function getName()
{
return 'user-name';
}
}
class Goods{
public function detail(User $user)
{
// 獲取用戶名
$username= $user->getName();
return $username;
}
}
$goods= new Goods();
// 先實例化User類
$user= new User();
echo $goods->detail($user);
?>
- 接收請求參數總結
//1.獲取請求對象
$request = request();
$request = \think\Request::instance();
$request = $this->request; //僅限於繼承了底層控制器的情況下
public function save(Request $request) //依賴注入
//2. 接收請求參數 param方法
$params = $request->param();
$params = input();
$params = request()->param();
$id = $request->param('id');
$id = input('id');
public function edit($id)//參數綁定