定制 avris/http 二次开发

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

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

avris/http

Composer 安装命令:

composer require avris/http

包简介

HTTP Request/Response abstraction

README 文档

README

HTTP Request/Response abstraction

Instalation

composer require avris/http

Usage

Request

What PHP puts in ugly superglobals $_SERVER, $_GET, $_POST, $_COOKIE and $_FILES, the Request class encapsulates in a nice object. Just do:

$request = Request::fromGlobals();

and you'll be able to elegantly access all the data and pass it along.

  • getMethod(): string
  • isPost(): bool
  • getScheme(): string
  • getHost(): string
  • getAbsoluteBase(): string
  • getBase(): string
  • getFrontController(): string
  • getUrl(): string
  • getCleanUrl(): string
  • getFullUrl(): string
  • getIp(bool $safe = true): string
  • isAjax(): bool
  • getQuery(): Bag -- $_GET
  • getData(): Bag -- $_POST
  • getCookies(): Bag
  • getServer(): Bag
  • getFiles(): FileBag
  • getHeaders(): HeaderBag
  • getBody(): string
  • getJsonBody()

For purposes of testing etc. you can also create a request object with some arbitrary data, for instance:

$request = Request::create('DELETE', '/posts/123', [], ['redirect' => '1']);

The abstraction layer for headers provides additional features, like getting the best Accept-* value.

Files in the request will be wrapped in an UploadedFile:

if ($request->isPost()) {
    $file = $request->getFiles()->get('attachment');
    $filename = $file->moveToRandom(__DIR__ . '/pics');
    $post->setAttachment($filename);
}

Response

In a similar fashon, the Response class encapsulates an HTTP Response. Your controller might for instance return:

return new Response(
    '<strong>OK</strong>',
    201,
    ['Content-Type' => 'text/html; charset=utf-8'],
    new CookieBag([
        new Cookie('foo', 'bar', new \DateTime('+10 minutes')),
    ]),
);

This object can then be passed on, modified, extended etc., without actually sending any data over HTTP. Only after you execute $response->send(), your headers, cookies, response code and content will be sent.

The library provides some nice helpers for creating more standard responses:

* `new JsonResponse(['foo' => bar])`
* `new RedirectResponse('/done')`
* `new FileResponse($post->getAttachmentPath(), 'image.png')`
* `new MemoryFileResponse($image->getContent())`
* `new StreamedResponse(function() { while (true) { echo 'heartbeat' . PHP_EOL; sleep(1); }})`

Session

Instead of using the $_SESSION superglobal, you can encapsulate it in a Session object. It works basically the same, but lets you write more elegant, object oriented, testable code, as well as easily exchange the standard session with a different one.

StoragelessSession is an alternative approach to standard HTTP sessions. It doesn't story any information on the server, instead sends it back and forth using cookies. (Therefore it should only be used for small amount of non-confidential data over a secure connection!)

$session = new StoragelessSession;
// ...
$session->onRequest($request); 
// ...
$foo = $session->get('foo'); 
$session->set('bar', $bar);
// ...
$session->onResponse($response);
// ...
$response->send();

Copyright

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固