使用方式

/**
* 如果直接示例化 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

[SF] Symfony 标准 HttpFoundation\Request 实现分析的更多相关文章

  1. sf中标准的分页功能介绍

    世上本无事,庸人自扰之.我喜欢一个相对比较安静的环境去学习和工作,希望在一个掉一根针的声音都能够听到的环境中,但是有时候往往相反,一片嘈杂,我改变不了周围的环境,只能改变自己,其实这些都没有什么,也许 ...

  2. 序列化与反序列化、def的介绍与快速使用、cbv源码分析、APIView与request对象分析

    今日内容概要 序列化与反序列化 def介绍和快速使用 cbv源码流程分析 drf之APIView和Request对象分析 内容详细 1.序列化和反序列化 # api接口开发 最核心最常见的一个过程就是 ...

  3. 《BI那点儿事》运用标准计分和离差——分析三国超一流统帅综合实力排名 绝对客观,数据说话

    数据分析基础概念:标准计分: 1.无论作为变量的满分为几分,其标准计分的平均数势必为0,而其标准差势必为1.2.无论作为变量的单位是什么,其标准计分的平均数势必为0,而其标准差势必为1.公式为: 离差 ...

  4. [SF] Symfony 组件 BrowserKit 原理

    直接看下面的注释中针对每一个文件的作用说明. <?php /** * BrowserKit - Make internal requests to your application. * * I ...

  5. [SF] Symfony 在 console 中结合 Workerman

    在web框架的console中,命令不再是直接指定入口文件,如以往 php test.php start,而是类似 php app/console do 的形式. workerman 对命令的解析是 ...

  6. laravel中的$request对象构造及请求生命周期

    laravel应用程序中index.php是所有请求的入口.当用户提交一个form或者访问一个网页时,首先由kernel捕捉到该session PHP运行环境下的用户数据, 生成一个request对象 ...

  7. symfony学习笔记1—简介

    1.symfony快速入门还是先看代码结构把,这个是拿到代码的第一印象,app/:整个应用的配置,模版,translations,这个可能是多语言文件什么,src/:项目php文件,vendor/:第 ...

  8. 跟我一起用Symfony写一个博客网站;

    我的微信公众号感兴趣的话可以扫一下, 或者加微信号   whenDreams 第一部分:基础设置,跑起一个页面-首页 第一步: composer create-project symfony/fram ...

  9. Symfony启动过程详细学习

    想了解symfony的启动过程,必须从启动文件(这里就以开发者模式)开始. <?php /* * web/app_dev.php */ $loader = require_once __DIR_ ...

随机推荐

  1. Galaxian 小蜜蜂

    发售年份 1979 平台 街机 开发商 南梦宫(Namco) 类型 射击 https://www.youtube.com/watch?v=XhYVcwhSWjI

  2. C#使用CH341 SPI模块读写SD卡

    SD卡相关CMD命令 ;//卡复位 ; ;//命令9 ,读CSD数据 ;//命令10,读CID数据 ;//命令12,停止数据传输 ;//命令16,设置SectorSize 应返回0x00 ;//命令1 ...

  3. Mxd文档更新比例尺

    在AE中,更新Mxd文档的比例尺,比较特殊.写代码以记录,更新比例尺代码如图所示: [DllImport("User32.dll")] public static extern i ...

  4. EasyTouch和NGUI的使用心得

    今天来写一写Unity3D中两个比较常用插件:EasyTouch和NGUI的学习心得.我用的版本分别是EasyTouch 3.1.1和NGUI 3.6.0,下面也是对这两个版本的学习心得. 1. Ea ...

  5. java 身份证工具类

    package com.app.wx.common.util; import org.apache.commons.lang3.StringUtils; import java.text.ParseE ...

  6. monitor.sh

    #!bin/bash message_counts="" succeed_counts="" all_succeed_counts="" f ...

  7. 防止shell script多次运行

    一个思路是在script初期检测系统中是否存在同名进程. ] then echo "This script is already running. Exit." else whil ...

  8. 报错:Failed on local exception: Host Details : local host is: "master/192.168.52.26"; dest

    报错现象 Failed on local exception: com.google.protobuf.InvalidProtocolBufferException: Protocol message ...

  9. Mysql 分组选择

    Mysql 分组选择 在其他的数据库中我们遇到分组选择的问题时,比如在分组中计算前10名的平均分 我们可以使用row_number()over() 比较方便的得到. 但是在mysql中,问题就被抛了出 ...

  10. k2datas 基础编程题,判断字符串是否有重复串

    package String; public class DuplicateString { public static boolean isDup(String s) throws Exceptio ...