Hyper中的 Request和Response


常見的Request和Response

請求相關

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use Hyperf\HttpMessage\Base\Request;
use Hyperf\HttpMessage\Server\Request;
use Hyperf\HttpServer\Contract\RequestInterface ;
use Hyperf\HttpServer\Request;
use Swoole\Http\Request;
use Swoole\Http2\Request;

響應相關

use Psr\Http\Message\ResponseInterface;
use Hyperf\HttpMessage\Base\Response;
use Hyperf\HttpMessage\Server\Response;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Response;
use Swoole\Http\Response;
use Swoole\Http2\Response;

其中

use Swoole\Http\Request;
use Swoole\Http2\Request;

use Swoole\Http\Response;
use Swoole\Http2\Response;

是Swoole提供的請求響應對象,主要是為Hyperf構建標准的PSR7 Request和Response對象提供元數據,我們不多做解釋,平時框架里不直接使用

 

請求

Psr\Http\Message\RequestInterface和Psr\Http\Message\ServerRequestInterface

這兩個接口是PSR7協議定義的關於HTTP消息對象的一些推薦規范的接口
ServerRequestInterface 是 RequestInterface 的一個子接口
RequestInterface是抽象概念上的請求接口
ServerRequestInterface是專屬於服務端的請求接口
Hyperf 的請求對象是基於PSR7實現的,也就是說hyperf里的Request對象都是
ServerRequestInterface和RequestInterface的實現類
(Hyperf\HttpMessage\Base\Request 和 Hyperf\HttpMessage\Server\Request)

Hyperf\HttpMessage\Base\Request和Hyperf\HttpMessage\Server\Request

Hyperf\HttpMessage\Base\Request是Psr\Http\Message\RequestInterface的實現
Hyperf\HttpMessage\Server\Request 是 Psr\Http\Message\ServerRequestInterface的實現
也就是說Hyperf\HttpServer\Request是由Hyperf\HttpMessage\Base\Request構建來的

Hyperf\HttpServer\Contract\RequestInterface和Hyperf\HttpServer\Request

Hyperf\HttpServer\Request是Hyperf\HttpServer\Contract\RequestInterface的實現類

 

Hyperf是一個協程框架,而Hyperf里面DI容器管理的對象都是長生命周期的對象。
假設我們再Hyperf里的一個Controller里使用了一個Request對象,而一個Request對象他跟隨的就是一個請求。
一個請求就是一個協程,而這個Controller是會在多個協程之間來回切換混着使用的。
所以假設我們將一個Request對象綁定在這個Controller的一個成員屬性上,就非常容易出現協程間數據混淆的問題。
因此Hyperf提供了Hyperf\HttpServer\Contract\RequestInterface,其目的在於保持在Controller的成員屬性上
直接Inject注入的這種用法同時又不會導致協程間的數據混淆。

實現方式:
Hyperf\HttpServer\Request核心代碼

public function query(?string $key = null, $default = null)
{
    if ($key === null) {
        return $this->getQueryParams();
    }
    return data_get($this->getQueryParams(), $key, $default);
}

public function getQueryParams()
{
    return $this->call(__FUNCTION__, func_get_args());
}


protected function call($name, $arguments)
{
    $request = $this->getRequest();
    if (! method_exists($request, $name)) {
        throw new \RuntimeException('Method not exist.');
    }
    return $request->{$name}(...$arguments);
}

protected function getRequest(): ServerRequestInterface
{
    return Context::get(ServerRequestInterface::class);
}

getRequest()方法返回的 Request對象是一個代理類,它代理的是ServerRequestInterface的儲存對象也就是Hyperf\HttpServer\Request對象

Hyperf\HttpServer\Request是一個標准的PSR7實現類,也綁定着每一次的用戶請求,也就是說Hyperf\HttpServer\Request是絕對不能再協程之間相互引用的。

它必須通過協程上下文來取得對應的Request對象。

Hyperf\HttpServer\Request幫助我們則很好地幫我們解決了這個問題,我們可以只需使用Hyperf\HttpServer\Request對象,由框架底層自動完成請求對象的選擇。

我們要獲Hyperf\HttpServer\Request對象,就必須Inject Hyperf\HttpServer\Contract\RequestInterface來得到這個代理對象

 

響應

Psr\Http\Message\ResponseInterface

PSR7約束的響應接口

Hyperf\HttpMessage\Base\Response

基於PSR7約束實現的響應對象,里面包含了Response的標准實現

Hyperf\HttpMessage\Server\Response

是Hyperf\HttpMessage\Base\Response的一個子類

它的主要目的是在服務端的情況下,將當前構建好的Response對象里面的內容響應給當前的請求

其中Hyperf\HttpMessage\Server\Response 下的send方法

將當前構建好的Response對象通過Swoole的Response對象將這些內容返回給這個請求

換句話說Hyperf\HttpMessage\Server\Response,就是Swoole\Http\Response與Hyperf\HttpMessage\Base\Response的一個橋接,來完成這個請求的響應

類似於Hyperf\HttpMessage\Server\Request 也是從協程上下文中獲取對應的響應對象,並做相關的操作

Hyperf\HttpMessage\Server\Response處理提供PSR7約束的方法之外,還提供了一些日常開發中常用的結果響應的方法

Hyperf\HttpServer\Contract\ResponseInterface

請求的代理類

Hyperf\HttpServer\Response

Hyperf\HttpServer\Contract\ResponseInterface的實現

實現方式類似

 

總結

請求:

存儲在協程上下文中的請求對象的key是Psr\Http\Message\ServerRequestInterface

對應的儲存在協程上下文中的對象是Hyperf\HttpMessage\Server\Request

在日常開發中使用的,注入的是Hyperf\HttpServer\Contract\RequestInterface接口類

Hyperf\HttpServer\Contract\RequestInterface實際的代理對象就是Hyperf\HttpServer\Request

 

響應相關類似請求

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM