[SF] Symfony 标准 HttpFoundation\Request 实现分析
使用方式
/**
* 如果直接示例化 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 实现分析的更多相关文章
- sf中标准的分页功能介绍
世上本无事,庸人自扰之.我喜欢一个相对比较安静的环境去学习和工作,希望在一个掉一根针的声音都能够听到的环境中,但是有时候往往相反,一片嘈杂,我改变不了周围的环境,只能改变自己,其实这些都没有什么,也许 ...
- 序列化与反序列化、def的介绍与快速使用、cbv源码分析、APIView与request对象分析
今日内容概要 序列化与反序列化 def介绍和快速使用 cbv源码流程分析 drf之APIView和Request对象分析 内容详细 1.序列化和反序列化 # api接口开发 最核心最常见的一个过程就是 ...
- 《BI那点儿事》运用标准计分和离差——分析三国超一流统帅综合实力排名 绝对客观,数据说话
数据分析基础概念:标准计分: 1.无论作为变量的满分为几分,其标准计分的平均数势必为0,而其标准差势必为1.2.无论作为变量的单位是什么,其标准计分的平均数势必为0,而其标准差势必为1.公式为: 离差 ...
- [SF] Symfony 组件 BrowserKit 原理
直接看下面的注释中针对每一个文件的作用说明. <?php /** * BrowserKit - Make internal requests to your application. * * I ...
- [SF] Symfony 在 console 中结合 Workerman
在web框架的console中,命令不再是直接指定入口文件,如以往 php test.php start,而是类似 php app/console do 的形式. workerman 对命令的解析是 ...
- laravel中的$request对象构造及请求生命周期
laravel应用程序中index.php是所有请求的入口.当用户提交一个form或者访问一个网页时,首先由kernel捕捉到该session PHP运行环境下的用户数据, 生成一个request对象 ...
- symfony学习笔记1—简介
1.symfony快速入门还是先看代码结构把,这个是拿到代码的第一印象,app/:整个应用的配置,模版,translations,这个可能是多语言文件什么,src/:项目php文件,vendor/:第 ...
- 跟我一起用Symfony写一个博客网站;
我的微信公众号感兴趣的话可以扫一下, 或者加微信号 whenDreams 第一部分:基础设置,跑起一个页面-首页 第一步: composer create-project symfony/fram ...
- Symfony启动过程详细学习
想了解symfony的启动过程,必须从启动文件(这里就以开发者模式)开始. <?php /* * web/app_dev.php */ $loader = require_once __DIR_ ...
随机推荐
- 1、Nexus安装
1.nexus 下载地址: https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.5-02-bundle.tar.g ...
- Oracle触发bug(cursor: mutex S),造成数据库服务器CPU接近100%---SQL子游标多版本问题
问题现象: 项目反馈系统反应非常缓慢,数据库服务器CPU接近100%! INSERT INTO GSPAudit1712(ID,TypeID,CategoryID,DateTime,UserID,Us ...
- 当鼠标经过表格数据行时颜色不同且奇偶行颜色也不同 (纯CSS)
<!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Prope ...
- TCL 引用同目录下其他脚本文件--source
方法一: source [file join [file dirname [info script]] Export_inp_by_loadstep.tcl] 方法二: source [file jo ...
- SSIS 中将csv 文件批量导出到excel 文件,并设置excel 文件中某些列的data column format 为Text
csv 文件是文本文件类型,但是打开csv 文件后(默认使用本地已经安装的excel 来打开excel 文件),默认显示出来的是general 类型(column data format)的数据, 这 ...
- 在 Docker 里跑 Java,你必须知道的那些事儿!(转)
原文 https://www.jianshu.com/p/0897d0581872 背景:众所周知,当我们执行没有任何调优参数(如“java-jar mypplication-fat.jar”)的 J ...
- Keras bug in model.predict
When I use Keras to predict behind a web service, it occurred an error. and the error message is lik ...
- rabbitmq (五)RPC
Remote Procedure Call or RPC(远程函数调用) 当我们需要在远程计算机上运行一个函数,并且等待结果的时候,我们用到RPC 在rabbitmq客户端使用call函数,发送RPC ...
- 统计每日单量MySQL语句
-- 每日单量 select DATE_FORMAT(createtime,'%Y-%m-%d') as days,count(*) count from ibt_shop_order group b ...
- JS 实现兼容IE图片向左或向右翻转
<!DOCTYPE HTML> <head> <title>JS实现图片向左向右翻转</title> <meta http-equiv=" ...