tekintian / http-foundation
从symfony/http-foundation完全精简独立出来的 HttpFoundation 组件。
Requires
- php: >=7.2.5
- symfony/polyfill-mbstring: ~1.1
- symfony/polyfill-php80: ^1.16
Suggests
- predis/predis: For using RedisRepository
This package is auto-updated.
Last update: 2024-09-17 04:30:45 UTC
README
从symfony/http-foundation完全精简独立出来的 HttpFoundation 组件。版本 5.4.x-dev
支持PHP7.2以上版本,php8.0 8.2也都支持
使用方法
# 安装 composer require tekintian/http-foundation # 可选安装 这个仅在需要使用 请求频率限制时才需要安装 composer require symfony/rate-limiter
<?php require_once('/path/to/vendor/autoload.php'); //Load with ClassLoader use Tekintian\HttpFoundation\Request; use Tekintian\HttpFoundation\Response; $request = Request::createFromGlobals(); $response = new Response(); ........
response
$response = new StreamedResponse($callback, Response::HTTP_OK, array( 'Content-Type' => 'text/event-stream', 'Cache-Control' => 'no-cache', 'X-Accel-Buffering' => 'no' // Disables FastCGI Buffering on Nginx )); if($this->allow_cors){ $response->headers->set('Access-Control-Allow-Origin', '*'); $response->headers->set('Access-Control-Allow-Credentials', 'true'); } if($this->use_chunked_encoding) $response->headers->set('Transfer-encoding', 'chunked');
Request
创建请求的最常见方式是使用createFromGlobals(),它几乎等同于更详尽但更灵活的__construct()调用
use Tekintian\HttpFoundation\Request;
$request = Request::createFromGlobals();
which is almost equivalent to the more verbose, but also more flexible, __construct() call
$request = new Request(
$_GET,
$_POST,
[],
$_COOKIE,
$_FILES,
$_SERVER
);
访问请求数据
请求对象包含有关客户端请求的信息。这些信息可以通过几个公共属性访问
- request: 等同于 $_POST;
- query: 等同于 $_GET ($request->query->get('name'));
- cookies: 等同于 $_COOKIE;
- attributes: 没有等效项 - 由您的应用程序用于存储其他数据(见下文);
- files: 等同于 $_FILES;
- server: 等同于 $_SERVER;
- headers: 主要等同于 $_SERVER 的子集 ($request->headers->get('User-Agent')).
每个属性都是 ParameterBag 实例(或其子类),这是一个数据持有类
- request: ParameterBag 或 InputBag 如果数据来自 $_POST 参数;
- query: InputBag;
- cookies: InputBag;
- attributes: ParameterBag;
- files: FileBag;
- server: ServerBag;
- headers: HeaderBag.
所有 ParameterBag 实例都有方法来检索和更新它们的数据
- all() 返回参数。
- keys() 返回参数键。
- replace() 用新集替换当前参数。
- add() 添加参数。
- get() 通过名称返回参数。
- set() 通过名称设置参数。
- has() 如果参数已定义,则返回 true。
- remove() 删除参数。
ParameterBag 实例还有一些用于过滤输入值的方法
-
getAlpha() 返回参数值的字母字符;
-
getAlnum() 返回参数值的字母和数字;
-
getBoolean() 返回转换为布尔值的参数值;
-
getDigits() 返回参数值的数字;
-
getInt() 返回转换为整数的参数值;
-
getEnum() 返回转换为 PHP 枚举的参数值;
-
getString() 返回作为字符串的参数值;
更多使用方法请参考官方文档:
https://symfony.com.cn/doc/current/components/http_foundation.html