日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php response响应,9. 响应 (Response)

發布時間:2025/3/15 php 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php response响应,9. 响应 (Response) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

## 為什么單獨創建一個Response?

跟`為什么要單獨創建一個Request` 一樣。

原因: 可以管理

如: 在 `swoole` 不應該用 `echo`, 因為 `swoole` 是 `cli` 運行,只會輸出在命令行。

`必須` 只有一個地方能 `輸出響應` 就是此篇的功能

確保有 `集中控制權` 是非常重要 !

(后面代碼有 `echo` 都是不規范的, 應該調用 `此篇` 的功能)

## 創建core/Response.php

```

namespace core;

class Response

{

protected $headers = []; // 要發送的請求頭

protected $content = ''; // 要發送的內容

protected $code = 200; // 發送狀態碼

public function sendContent() // 發送內容

{

echo $this->content;

}

public function sendHeaders() // 發送請求頭

{

foreach ($this->headers as $key => $header)

header($key.': '.$header);

}

public function send() // 發送

{

$this->sendHeaders();

$this->sendContent();

return $this;

}

public function setContent($content) // 設置內容

{

if( is_array($content))

$content = json_encode($content);

$this->content = $content;

return $this;

}

public function getContent() // 獲取內容

{

return $this->content;

}

public function getStatusCode() // 獲取狀態碼

{

return $this->code;

}

public function setCode(int $code) // 設置狀態碼

{

$this->code = $code;

return $this;

}

}

```

## 在容器綁定Response類

編輯 `app.php`的`register` 方法

![](https://img.kancloud.cn/f0/ad/f0ad878555ff9efa213a1c6011f7c17e_606x187.png)

## 編輯index.php

![](https://img.kancloud.cn/56/90/5690de0edbfdf9f0665cdaf48566e739_824x519.png)

![](https://img.kancloud.cn/06/3b/063bb94f40ec4253318fc802cdfef561_612x175.png)

總結

以上是生活随笔為你收集整理的php response响应,9. 响应 (Response)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。