anax / request
Anax 请求模块,关于请求的详细信息。
Requires
- php: ^7.2
Requires (Dev)
- anax/di: ^2.0.4
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2024-09-06 08:30:53 UTC
README
为包装所有请求相关信息的 Anax 请求模块。
该模块本质上封装了对 $_GET, $_POST, $_SERVER
的访问,并从请求 URI 计算出 URL 细节(当前、站点、基本、路由路径)。
该模块提供了一种统一的框架来访问这些全局变量,并在使用该模块进行单元测试时提供了一种注入特定设置的方式。
目录
- 类、接口、特性
- 异常
- 配置文件
- DI 服务
- 在 Anax 框架中的通用使用
- 作为框架服务访问
- 创建和初始化对象
- 提取 URL 和路由部分
- 获取和设置
$_SERVER
- 获取和设置
$_GET
- 获取和设置
$_POST
- 获取和设置请求体
类、接口、特性
存在以下类、接口和特性。
异常
通过 Anax\Request\Exception
抛出特定于模块的异常。
配置文件
此模块没有配置文件。
DI 服务
该模块在 $di
中作为框架服务创建。您可以在配置文件 config/di/request.php
中查看详细信息。
它可能看起来像这样。
/** * Configuration file for request service. */ return [ // Services to add to the container. "services" => [ "request" => [ "shared" => true, "callback" => function () { $obj = new \Anax\Request\Request(); $obj->init(); return $obj; } ], ], ];
- 对象被创建为共享资源。
- init 方法从环境中读取信息以找到请求的 URL。
服务是延迟加载的,直到使用时才创建。
在 Anax 框架中的通用使用
请求服务是 Anax 框架中的必需服务,它是处理请求时使用的第一个服务。
以下是接收请求、将其映射到路由并返回响应的一般流程。这可以在 Anax 安装的 frontcontroller htdocs/index.php
中找到。
// Leave to router to match incoming request to routes $response = $di->get("router")->handle( $di->get("request")->getRoute(), $di->get("request")->getMethod() ); // Send the HTTP response with headers and body $di->get("response")->send($response);
请求用于获取请求方法和路由路径,这些由路由服务用于查找路由的回调。然后,每个回调可以返回一个响应,该响应通过响应服务发送。
作为框架服务访问
您可以将模块作为框架服务访问。
# $app style $app->request->getRoute(); # $di style, two alternatives $di->get("request")->getRoute(); $request = $di->get("request"); $request->getRoute();
创建和初始化对象
以下是创建对象的方法。这通常在框架中作为 $di
中的服务完成。
# Create an object $request = new \Anax\Request\Request(); # Set (reset) globals, useful in unit testing # when not using $_GET, $_POST, $_SERVER $request->setGlobals(); # Init the class by extracting url parts and # route path. $request->init();
提取 URL 和路由部分
当对象被初始化时,您可以从其中提取 URL 和路由部分。这是基于当前 URL 的。
# Get site url including scheme, host and port. $request->getSiteUrl(); # Get base url including site url and path to current index.php. $request->getBaseUrl(); # Get current url as base url and attach # the query string. $request->getCurrentUrl(); # Get script name, index.php or other. $request->getScriptName(); # Get HTTP request method, for example # GET, POST, PUT, DELETE. $request->getMethod(); # Get route path as a string. $request->getRoute(); # Get route path parts in an array. $request->getRouteParts();
获取和设置 $_SERVER
您可以在 PHP 全局变量 $_SERVER
中获取和设置值。
# Read a value $value = $request->getServer($key); # Read all values as an key value array $array = $request->getServer(); # Read a value and use $default if $key is not set. $value = $request->getServer($key, $default); # Set a value $request->setServer($key, $value);
您正在读取和设置 $_SERVER
的副本中的值,因此您实际上并没有编辑全局变量,只是编辑了类内部的内部表示。
获取和设置 $_GET
您可以在 PHP 全局变量 $_GET
中获取和设置值。
# Read a value $value = $request->getGet($key); # Read all values as an key value array $array = $request->getGet(); # Read a value and use $default if $key is not set. $value = $request->getGet($key, $default); # Set a value $request->setGet($key, $value);
您正在读取和设置 $_GET
的副本中的值,因此您实际上并没有编辑全局变量,只是编辑了类内部的内部表示。
获取和设置 $_POST
您可以在 PHP 全局变量 $_POST
中获取和设置值。
# Read a value $value = $request->getPost($key); # Read all values as an key value array $array = $request->getGet(); # Read a value and use $default if $key is not set. $value = $request->getPost($key, $default); # Set a value $request->setPost($key, $value);
您正在读取和设置 $_POST
的副本中的值,因此您实际上并没有编辑全局变量,只是编辑了类内部的内部表示。
获取和设置请求体
您可以在HTTP请求体中获取和设置值。有时HTTP请求体用于向路由发送参数。
# Read the body $request->getBody(); # Read the body and treat it as json $request->getBodyAsJson() # Set the body $request->setBody($content);
您正在设置一个实际体副本中的值,因此您实际上并没有编辑它,只是编辑了类内的内部表示。
许可证
本软件携带MIT许可证。有关详细信息,请参阅LICENSE.txt。
.
..: Copyright (c) 2013 - 2020 Mikael Roos, mos@dbwebb.se