telegraph/middleware 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

telegraph/middleware

Composer 安装命令:

composer require telegraph/middleware

包简介

Telegraph-compatible middleware decorators.

README 文档

README

This package includes the following Telegraph-compatible middleware:

  • ExceptionHandler to handle exceptions from subsequent middleware
  • FormContentHandler to deserialize the URL-encoded payload of a PSR-7 request
  • JsonContentHandler to deserialize the JSON payload of a PSR-7 request
  • ResponseSender to send a PSR-7 response
  • SessionHeadersHandler to manage session headers "manually", instead of PHP managing them automatically

This package is installable and PSR-4 autoloadable via Composer as telegraph/middleware.

ExceptionHandler

Similarly, the ExceptionHandler does what it sound like: it catches any exceptions that bubble up through the subsequent middleware decorators.

The ExceptionHandler does nothing with the $request , and passes it directly to $next inside a try/catch block. If no exception bubbles up, it returns the $response from $next. However, if it catches an exception, it returns an entirely new $response object with the exception message and an HTTP 500 status code. It then returns the new $response object.

The ExceptionHandler is intended to go near the top of the Telegraph queue, but after the ResponseSender, so that the ResponseSender can then send the returned $response.

To add the ExceptionHandler to your queue, instantiate it directly with an empty $response implementation object ...

$queue[] = new \Telegraph\Middleware\ExceptionHandler(new ResponseImplementation());

... or use a $resolver of your choice to instantiate it from the $queue.

FormContentHandler

FormContentHandler works almost identically to JsonContentHandler (below), but parses payloads of requests that have application/x-www-form-urlencoded as the Content-Type.

JsonContentHandler

Again, the JsonContentHandler does what it sounds like: it deserializes the JSON payload of a PSR-7 request object and makes the parameters available in subsequent middleware decorators.

The JsonContentHandler checks the incoming request for a method other than GET and for an application/json or application/vnd.api+json Content-Type header. If it finds both of these, it parses the JSON and makes it available as the parsed body of the $request before passing it to $next. If the method is GET or the Content-Type header defines a different mime type, the JsonContentHandler ignores the $request and continues the chain.

To add the JsonContentHandler to your queue, instantiate it directly...

$queue[] = new \Telegraph\Middleware\JsonContentHandler();

... or use a $resolver of your choice to instantiate it from the $queue.

To access the decoded parameters in subsequent middleware, use the getParsedBody() method of the $request

$decodedJsonData = $request->getParsedBody();

ResponseSender

The ResponseSender does just what it sounds like: it sends the PSR-7 response object.

The ResponseSender does nothing with the $request, passing it immediately to $next. Afterwards, it takes the returned $response and sends it using header() and echo, and returns the sent $response.

The ResponseSender is intended to go at the top of the Telegraph queue, so that it is the middleware with the last opportunity to do something with the returned response.

To add the ResponseSender to your Telegraph queue, instantiate it directly ...

$queue[] = new \Telegraph\Middleware\ResponseSender();

... or use a $resolver of your choice to instantiate it from the $queue.

SessionHeadersHandler

Normally, PHP will send out headers for you automatically when you call session_start(). However, this means the headers are not being sent as part of the PSR-7 response object, and are thus outside your control. This handler puts them back under your control by placing the relevant headers in the PSR-7 response; its behavior is almost identical to the native PHP automatic session headers behavior.

NOTE: For this middleware to work, you must disable the PHP session header management ini settings. For example:

ini_set('session.use_trans_sid', false);
ini_set('session.use_cookies', false);
ini_set('session.use_only_cookies', true);
ini_set('session.cache_limiter', '');

If you do not, the handler will throw a RuntimeException.

To add the SessionHeadersHandler to your queue, instantiate it directly...

$queue[] = new \Telegraph\Middleware\SessionHeadersHandler();

... or use a $resolver of your choice to instantiate it from the $queue.

When instantiating, you can pass a cache limiter value as the first constructor parameter. The allowed values are 'nocache', 'public', 'private_no_cache', or 'priviate'. If you want no cache limiter header at all, pass an empty string ''. The default is 'nocache'.

You can also pass a cache expire value, in minutes, as the second constructor parameter. The default is 180 minutes.

telegraph/middleware 适用场景与选型建议

telegraph/middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 9, 最近一次更新时间为 2016 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-29