定制 modulework/http 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

modulework/http

Composer 安装命令:

composer require modulework/http

包简介

HTTP package for the MODULEWork Framework

关键字:

README 文档

README

Latest Stable Version Build Status

The HTTP Package of the Modulework Framework.

It provides a convient way of handling HTTP request and HTTP response.

So for example you could already create a application with these two classes:

$req = Request::makeFromGlobals();
    
$content = 'Hello ' . $req->query->get('name', 'Stranger');
    
$res = Response::make($content);
$res->send();

This will app will great every vistor with their name or if the vistor didn' t provide the name in the query string it will fallback to "Stranger".

Of course this is a very basic example but it can do a lot more!

We could expand this and save the name into a cookie:

$req = Request::makeFromGlobals();
    
$name = $req->query->get('name', Stranger);
$content = 'Hello ' . $name;
    
$res = Response::make($content);
$res->addCookie(Cookie::make(
		'name',
		$name
));
$res->send();

Or check if the method is GET or POST, so a user could also POST it' s name:

$req = Request::makeFromGlobals();

$name = $req->query->get('name',
	$req->request->get('name', 'Stranger')
	);
// Or just getting the method:
$method = $req->getMethod();

$content = 'Hello ' . $name;

$res = Response::make($content);
$res->addCookie(Cookie::make(
		'name',
		$name
		));
$res->send();

Or display the client' s IP:

echo Request::makeFromGlobals()->getClientIp();

Now we also send a cookie a too the user. But we can do even more:

$res = Response::make()
->setContent('Foo')
->setStatusCode(200)
->addHeader('Expire', 'never')
->setDate(new DateTime)
->addCookie(Cookie::make('foo')
	->setValue('bar')
	->setSecure(false)
	->setHttpOnly(false)
)
->prepare($request)
->send();

Chained methods! Custom Headers! Custom Status Codes! And much more!

The Response class is intelligent enough to set the status code, if a redirect is issued:

$res = Response::make()
->addHeader('Location', 'foo.bar')
->prepare($request)
->send()

Will result in this header

HTTP/1.0 302 Found
Location: foo.bar
Date [...]

But the Request class can do even more, we can use it for very basic routing:

$req = Request::makeFromGlobals();
if ('/' == $req->getPath()) {
    echo "You are on the homepage"
} elseif ('/foo/bar' == $req->getPath()) {
    echo "You are on the bar page of foo!"
}

This is very basic and not best practice, but it shows for what we can use this class for!

There are also some more Response classes, like the RedirectResponse and JsonResponse class.

Here is an usage example:

$res = RedirectResponse::make('http://foo.bar')
->withCookie(Cookie::make('foo'))
->prepare(Request::makeFromGlobals)
->send();

As you can see very easy to use. NOTE! This is just a wrapper for a normal Response. You could also do this:

$res = Response::make('<extra>', 302, array('Location' => 'http://foo.bar'))
->addCookie(Cookie::make('foo'))
->prepare(Request::makeFromGlobals)
->send();

The only thing what is not shown in the alternative way is the HTML meta redirect (in case the header doesn' t fire).

The JsonResponse class is pretty straight forward as well:

$res = JsonResponse::make(array('foo' => 'bar'))
->prepare(Request::makeFromGlobals)
->send();

This would result in this response:

HTTP/1.0 200 OK
Content-Type application/json
Date [...]

{"foo": "bar"}

Pretty nifty, eeh!!?

modulework/http 适用场景与选型建议

modulework/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 1, 最近一次更新时间为 2013 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-07-21