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请求合并
# 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变量

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)//参数绑定