承接 anax/request 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

anax/request

Composer 安装命令:

composer require anax/request

包简介

Anax Request module, details on the request.

README 文档

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 Request module for wrapping all request related information.

The module essentially wraps access to $_GET, $_POST, $_SERVER, information send through the HTTP body and calculates url details (current, site, base, route path) from the request uri.

The module provides a framework unified way to access these global variables and it provides a way to inject a certain setup when using the module for unit testing.

Table of content

Class, interface, trait

The following classes, interfaces and traits exists.

Class, interface, trait Description
Anax\Request\Request Wrapper class for request details and related.

Exceptions

Module specific exceptions are thrown through Anax\Request\Exception.

Configuration file

There is no configuration file for this module.

DI service

The module is created as a framework service within $di. You can see the details in the configuration file config/di/request.php.

It can look like this.

/**
 * 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;
            }
        ],
    ],
];
  1. The object is created as a shared resource.
  2. The init-method reads information from the environment to find out the url of the request.

The service is lazy loaded and not created until it is used.

General usage within the Anax framework

The request service is a mandatory service within the Anax framework and it is the first service used when handling a request.

Here is the general flow for receiving a request, mapping it to a route and returning a response. This is found in the frontcontroller htdocs/index.php of an Anax installation.

// 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);

The request is used to get the request method and the route path, these are used by the router service to find a callback for the route. Each callback can then return a response which is sent through the response service.

Access as framework service

You can access the module as a framework service.

# $app style
$app->request->getRoute();

# $di style, two alternatives
$di->get("request")->getRoute();

$request = $di->get("request");
$request->getRoute();

Create and init an object

This is how the object can be created. This is usually done within the framework as a sevice in $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();

Extract url and route parts

When the object is initiated you can extract url and route parts from it. This is based on the current 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();

Get and set $_SERVER

You can get and set values in the PHP global variable $_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);

You are reading and setting values in a copy of $_SERVER, so you are not actually editing the global variable, just the internal representation within the class.

Get and set $_GET

You can get and set values in the PHP global variable $_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);

You are reading and setting values in a copy of $_GET, so you are not actually editing the global variable, just the internal representation within the class.

Get and set $_POST

You can get and set values in the PHP global variable $_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);

You are reading and setting values in a copy of $_POST, so you are not actually editing the global variable, just the internal representation within the class.

Get and set request body

You can get and set the value in the HTTP request body. Sometimes the HTTP request body is used to send parameters to an route.

# Read the body
$request->getBody();

# Read the body and treat it as json
$request->getBodyAsJson()

# Set the body
$request->setBody($content);

You are setting values in a copy of the actual body, so you are not actually editing it, just the internal representation within the class.

License

This software carries a MIT license. See LICENSE.txt for details.

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

anax/request 适用场景与选型建议

anax/request 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.25k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「mvc」 「boilerplate」 「micro」 「education」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 anax/request 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 anax/request 我们能提供哪些服务?
定制开发 / 二次开发

基于 anax/request 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20.25k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 4
  • 依赖项目数: 41
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-03