一、Request
对象
thinkphp提供了Request
对象,其可以
-
支持对全局输入变量的检测、获取和安全过滤
-
支持获取包括
$_GET
、$_POST
、$_REQUEST
、$_SERVER
、$_SESSION
、$_COOKIE
、$_ENV
等系统变量,以及文件上传信息
具体参考:https://www.kancloud.cn/manual/thinkphp6_0/1037519
二、可以通过Request::param获取所有输入参数
PARAM
类型变量是框架提供的用于自动识别当前请求的一种变量获取方式,是系统推荐的获取请求参数的方法。
- 新建一个html页面
app/test/view/User/loginsimple.html
<!DOCTYPE html><html lang="en"><head></head><body> <form method="post" action="/index.php/test/User/dologin?logintype=2" > <input type="text" name="username"><br> <input type="text" name="password"><br> <input type="submit" value="提交"> </form> </body>
</html>
注意:提交路径为/index.php/test/User/dologin/func/login?logintype=2

- 新建一个控制器函数
app\test\Controller\User.php
引入
use think\facade\Request;
函数
<?phpnamespace app\test\controller;
use app\BaseController;// 添加引用use think\facade\View;use think\facade\Request;
class User extends BaseController{ // 登录页 public function loginsimple(){ // 模板输出 return View::fetch('User/loginsimple'); }
// 登录 public function dologin(){ // 静态调用 // 获取当前请求get中的logintype变量 print_r(Request::param('logintype')); print_r('<br/>'); // 获取当前请求get中的路径参数func变量 print_r(Request::param('func')); print_r('<br/>'); // 获取当前请求post中的name变量 print_r(Request::param('username')); print_r('<br/>'); // 获取当前请求的所有变量(经过过滤) print_r(Request::param()); print_r('<br/>'); // 获取当前请求未经过滤的所有变量 print_r(Request::param(false)); print_r('<br/>'); // 获取部分变量 print_r(Request::param(['username', 'email'])); }
}

- 测试

点击提交之后

可以看到,Request::param成功提取到了get中参数、url中的路径参数、post中的参数