anax/response

Anax 响应模块,用于创建 HTTP 响应。

v2.0.2 2020-10-27 13:15 UTC

README

Latest Stable Version Join the chat at https://gitter.im/mosbth/anax

Build Status CircleCI

Build Status Scrutinizer Code Quality Code Coverage

Maintainability Codacy Badge

Anax 响应模块,用于发送 HTTP 响应。

该模块用于从 Anax 框架发送 HTTP 响应,包括状态码、头信息和正文。

目录

类、接口、特性

存在以下类、接口和特性。

异常

特定于模块的异常通过 Anax\Response\Exception 抛出。

配置文件

此模块没有配置文件。

依赖注入服务

该模块在 $di 中作为框架服务创建。您可以在配置文件 config/di/response.php 中查看详细信息。

它可以看起来像这样。

/**
 * Configuration file for DI container.
 */
return [
    // Services to add to the container.
    "services" => [
        "response" => [
            "shared" => true,
            //"callback" => "\Anax\Response\Response",
            "callback" => function () {
                $obj = new \Anax\Response\ResponseUtility();
                $obj->setDI($this);
                return $obj;
            }
        ],
    ],
];
  1. 对象作为共享资源创建。
  2. 当使用 ResponseUtility 时,依赖注入 (DI) 被注入到模块中。

服务是延迟加载的,直到使用时才创建。

在 Anax 框架中的通用使用

响应服务是 Anax 框架中的必备服务,并且是处理框架请求时第一个使用的服务。

以下是接收请求、将其映射到路由并返回响应的一般流程。这可以在 Anax 安装的前端控制器 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->response->redirectSelf();

# $di style, two alternatives
$di->get("response")->redirectSelf();

$response = $di->get("response");
$response->redirectSelf();

创建、初始化和使用对象

这是创建对象的方式。这通常在框架中作为 $di 中的服务完成。

# Create an object
response = new \Anax\Response\Response();

# Send a response
response->send("Some response");
response->sendJson(["key" => "Some value"]);
response->redirect($absoluteUrl);

使用 ResponseUtility 的额外好处是这个类通过 DI 注入,使得在框架中使用 URL 的重定向方法变得更加容易。

# Create an object
response = new \Anax\Response\ResponseUtility();

# Do a redirect
response->redirect("game/init");
response->redirectSelf();

许可证

本软件携带 MIT 许可证。有关详细信息,请参阅 LICENSE.txt

 .  
..:  Copyright (c) 2013 - 2019 Mikael Roos, mos@dbwebb.se