承接 amphp/http-server-router 相关项目开发

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

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

amphp/http-server-router

Composer 安装命令:

composer require amphp/http-server-router

包简介

Routes to request handlers based on HTTP method and path for amphp/http-server.

README 文档

README

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. This package provides a routing RequestHandler for amphp/http-server based on the request path and method using FastRoute.

Installation

This package can be installed as a Composer dependency. You should specify amphp/http-server as a separate dependency.

composer require amphp/http-server-router

Usage

Router implements RequestHandler, which is used by an HttpServer to handle incoming requests. Incoming requests are routed by Router to other RequestHandlers based on the request path.

Routes can be defined using the addRoute($method, $uri, $requestHandler) method.

public function addRoute(
    string $method,
    string $uri,
    RequestHandler $requestHandler
): void

Matched route arguments are available in the request attributes under the Amp\Http\Server\Router key as an associative array. Please read the FastRoute documentation on how to define placeholders.

Middleware

You can stack all routes with a common set of middleware using addMiddleware($middleware). Each middleware is called in the order of the addMiddleware() calls. The router will not invoke any middleware for the fallback handler.

public function addMiddleware(Middleware $middleware): void

Note Per-route middleware can be added by using Amp\Http\Server\Middleware\stackMiddleware() before passing the RequestHandler to addRoute().

Fallback

If no routes match a request path, you can specify another instance of RequestHandler which will handle any unmatched routes. If no fallback handler is provided, a 404 response will be returned using the instance of ErrorHandler provided to the Router constructor.

public function setFallback(RequestHandler $requestHandler): void

Note Middleware defined by Router::addMiddleware() will not be invoked when a request is forwarded to fallback handler. Use Amp\Http\Server\Middleware\stackMiddleware() to wrap the fallback request handler with any middlewares needed first.

Example

use Amp\Http\HttpStatus;
use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\Router;
use Amp\Http\Server\SocketHttpServer;

// $logger is an instance of a PSR-3 logger.
$server = SocketHttpServer::createForDirectAccess($logger);
$errorHandler = new DefaultErrorHandler();

$router = new Router($server, $logger, $errorHandler);

$router->addRoute('GET', '/', new ClosureRequestHandler(
    function () {
        return new Response(
            status: HttpStatus::OK,
            headers: ['content-type' => 'text/plain'],
            body: 'Hello, world!',
        );
    },
));

$router->addRoute('GET', '/{name}', new ClosureRequestHandler(
    function (Request $request) {
        $args = $request->getAttribute(Router::class);
        return new Response(
            status: HttpStatus::OK,
            headers: ['content-type' => 'text/plain'],
            body: "Hello, {$args['name']}!",
        );
    },
));

$server->expose('0.0.0.0:1337');

$server->start($router, $errorHandler);

// Serve requests until SIGINT or SIGTERM is received by the process.
Amp\trapSignal([SIGINT, SIGTERM]);

$server->stop();

A full example is found in examples/hello-world.php.

php examples/hello-world.php

Limitations

The Router will decode the URI path before matching. This will also decode any forward slashes (/), which might result in unexpected matching for URIs with encoded slashes. FastRoute placeholders match path segments by default, which are separated by slashes. That means a route like /token/{token} won't match if the token contains an encoded slash. You can work around this limitation by using a custom regular expression for the placeholder like /token/{token:.+}.

Security

If you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.

License

The MIT License (MIT). Please see LICENSE for more information.

amphp/http-server-router 适用场景与选型建议

amphp/http-server-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 529.19k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2018 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 amphp/http-server-router 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 529.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 39
  • 点击次数: 22
  • 依赖项目数: 36
  • 推荐数: 0

GitHub 信息

  • Stars: 39
  • Watchers: 7
  • Forks: 6
  • 开发语言: PHP

其他信息

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