用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
### 为什么要单独创建一个Request 禁止使用`$_SERVER`,因为这个管理不了。 在 `swoole (虽然有)` `phpunit (cli模式)` 运行怎么办? 可是没有 `$_SERVER` `$_POST` .... 等超全局变量的。 (`swoole` 有`$_SERVER` ,但是更加禁止使用) 单独创建的原因: 可以管理 ## 编辑composer.json 添加映射 ``` "core\\": "core/" ``` ![](https://img.kancloud.cn/0a/15/0a158d29a6d796634b9112b380b329b0_383x176.png) 是不是多此一举? `composer` 自动加载并不会识别 `core`是那个目录。 `tp5`的 ![](https://img.kancloud.cn/5a/05/5a05d91c99a5afed2ff549816e718d61_244x76.png) ## 关于违背psr规范 `request` 轮子 `应该` 遵守[# PSR-7 HTTP 消息接口规范](https://learnku.com/docs/psr/psr-7-http-message/1616) 你可以在这里看到 `psr-7` 的代码: https://github.com/php-fig/http-message 违背原因: 太多方法了,我是不会实现这些接口的,因为对于教程,会增加非常多的代码。 ## 创建core/request/RequestInterface.php ``` <?php namespace core\request; interface RequestInterface { public function __construct($uri, $method, $headers); // 初始化 public static function create($uri, $method, $headers); // 创建request对象 public function getUri(); // 获取请求url public function getMethod(); // 获取请求方法 public function getHeader(); // 获取请求头 } ``` ## 创建 core/request/PhpRequest.php ``` <?php namespace core\request; class PhpRequest implements RequestInterface { protected $url; protected $method; protected $headers; public function __construct($uri,$method,$headers) { $this->uri = $uri; $this->method = $method; $this->headers = $headers; } // 创建一个请求 public static function create($uri,$method,$headers = []) { return new static($uri, $method, $headers); // new 自己 } public function getUri() { return $this->uri; } public function getMethod() { return $this->method; } public function getHeader() { } } ``` ### 运行 ![](https://img.kancloud.cn/cf/1a/cf1ae28650caadd7ca7ffe44c9da645a_794x419.png) ![](https://img.kancloud.cn/47/09/4709e545d198bac2b5516c93fac1834a_543x220.png)