Koa::Request

Request组件是对swoole_http_request的简单封装:

  1. <?php
  2. class Request
  3. {
  4. /** @var Application */
  5. public $app;
  6. /** @var \swoole_http_request */
  7. public $req;
  8. /** @var \swoole_http_response */
  9. public $res;
  10. /** @var Context */
  11. public $ctx;
  12. /** @var Response */
  13. public $response;
  14. /** @var string */
  15. public $originalUrl;
  16. /** @var string */
  17. public $ip;
  18. public function __construct(Application $app, Context $ctx,
  19. \swoole_http_request $req, \swoole_http_response $res)
  20. {
  21. $this->app = $app;
  22. $this->ctx = $ctx;
  23. $this->req = $req;
  24. $this->res = $res;
  25. }
  26. public function __get($name)
  27. {
  28. switch ($name) {
  29. case "rawcontent":
  30. return $this->req->rawContent();
  31. case "post":
  32. return isset($this->req->post) ? $this->req->post : [];
  33. case "get":
  34. return isset($this->req->get) ? $this->req->get : [];
  35. case "cookie":
  36. case "cookies":
  37. return isset($this->req->cookie) ? $this->req->cookie : [];
  38. case "request":
  39. return isset($this->req->request) ? $this->req->request : [];
  40. case "header":
  41. case "headers":
  42. return isset($this->req->header) ? $this->req->header : [];
  43. case "files":
  44. return isset($this->req->files) ? $this->req->files : [];
  45. case "method":
  46. return $this->req->server["request_method"];
  47. case "url":
  48. case "origin":
  49. return $this->req->server["request_uri"];
  50. case "path":
  51. return isset($this->req->server["path_info"]) ? $this->req->server["path_info"] : "";
  52. case "query":
  53. case "querystring":
  54. return isset($this->req->server["query_string"]) ? $this->req->server["query_string"] : "";
  55. case "host":
  56. case "hostname":
  57. return isset($this->req->header["host"]) ? $this->req->header["host"] : "";
  58. case "protocol":
  59. return $this->req->server["server_protocol"];
  60. default:
  61. return $this->req->$name;
  62. }
  63. }
  64. }