使用方式

/**
* 如果直接示例化 Request 默认是没有参数的,可以自己传入
* 本方法将 PHP 超全局变量作为参数然后实例化自身(Request)进行初始化。
*/
$request = Request::createFromGlobals();

表面的 Request 对象格式

+ 是公开属性,# 是受保护属性,- 是私有属性

源码中 Request 参数的初始化过程

    /**
* Sets the parameters for this request.
*
* This method also re-initializes all properties.
*
* @param array $query The GET parameters
* @param array $request The POST parameters
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
* @param array $cookies The COOKIE parameters
* @param array $files The FILES parameters
* @param array $server The SERVER parameters
* @param string|resource $content The raw body data
*/
public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
{
$this->request = new ParameterBag($request);
$this->query = new ParameterBag($query);
$this->attributes = new ParameterBag($attributes);
$this->cookies = new ParameterBag($cookies);
$this->files = new FileBag($files);
$this->server = new ServerBag($server);
$this->headers = new HeaderBag($this->server->getHeaders()); $this->content = $content;
$this->languages = null;
$this->charsets = null;
$this->encodings = null;
$this->acceptableContentTypes = null;
$this->pathInfo = null;
$this->requestUri = null;
$this->baseUrl = null;
$this->basePath = null;
$this->method = null;
$this->format = null;
}

源码中 ParameterBag 参数包装细节

class ParameterBag implements \IteratorAggregate, \Countable
{
/**
* Parameter storage.
*/
protected $parameters; /**
* @param array $parameters An array of parameters
*/
public function __construct(array $parameters = array())
{
$this->parameters = $parameters;
} /**
* Returns the parameters.
*
* @return array An array of parameters
*/
public function all()
{
return $this->parameters;
} // 其它实现的方法
// ...

小结

通过以上了解,完全透过 OOP 方式可以访问请求过程中的任何参数。

$request->attributes->get('q');
$request->server->get('SCRIPT_NAME');
$request->query->all( );
$request->request->keys();
$request->cookies->remove('sc');
$request->get('q'); # 依次从 attributes,query,request 检测是否有key的值,有就返回;兼容 get、post 方法时使用,否则建议访问对应公开属性上的方法,例如:$request->query->get('q');

并且 Request 提供了很多封装的便捷方法。

$request->getScriptName(); # 等同 $request->server->get('SCRIPT_NAME');

Link:https://www.cnblogs.com/farwish/p/9615790.html

最新文章

  1. iOS中的通知
  2. Antenna Placement poj 3020(匹配)
  3. java笔记--查看和修改线程名称
  4. RabbitMQ 基本概念介绍-----转载
  5. jQuery多库共存处理
  6. hdu 4876(剪枝+暴力)
  7. javascript之处理Ajax错误
  8. Android权限安全(9)Android权限特点及权限管理服务AppOps Service
  9. I.MX6 android 禁止低电量自动关机
  10. DDD:四色原型中Role的 “六” 种实现方式和PHP的Swoole扩展
  11. blob的存储与读取
  12. NoRouteToHostException
  13. arcgis point 随着 line类型的轨迹运动的动画
  14. CentOS7 安装PHP7的redis扩展:
  15. Checkpoints(第十一届河南省省赛真题)
  16. 洛谷 P2051 [AHOI2009]中国象棋 解题报告
  17. 使用 IntraWeb (9) - JavaScript
  18. C# FileStream进行FTP服务上传文件和下载文件
  19. eclipse javaee 插件安装
  20. Linux mysql 命令

热门文章

  1. 如何安装私有 npm 包?
  2. 关于js中原生构造函数的继承
  3. Redis类的源码使用
  4. Hello 2019
  5. MSDE2008安装备忘
  6. VSS2005源代码管理启用http方式
  7. Composer 的基本使用
  8. cassert(assert.h)——1个
  9. [UE4]让Spline具象化
  10. Unity Input,生命周期,Light,获取组件