久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 CakePHP 3.4 輸出自定義 HTTP 正文內容?回聲

How to output custom HTTP body contents with CakePHP 3.4? Echoing causes quot;Unable to emit headersquot; error(如何使用 CakePHP 3.4 輸出自定義 HTTP 正文內容?回聲導致“無法發出標頭錯誤) - IT屋-程序員軟件開發技
本文介紹了如何使用 CakePHP 3.4 輸出自定義 HTTP 正文內容?回聲導致“無法發出標頭"錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

使用 CakePHP 3.4、PHP 7.0.

Using CakePHP 3.4, PHP 7.0.

我正在嘗試做一個非常簡單的控制器方法來輸出一些 JSON.它正在輸出無法修改標題...".

I'm attempting to do a really simple controller method to output some JSON. It is outputting "Cannot modify headers...".

public function test() {
    $this->autoRender = false;
    echo json_encode(['method' => __METHOD__, 'class' => get_called_class()]);
}

瀏覽器輸出

{"method":"App\Controller\SomeController::test", "class":"App\Controller\SomeController"}

Warning (512): Unable to emit headers. Headers sent in file=...
Warning (2): Cannot modify header information - headers already sent by (output started at ...)
Warning (2): Cannot modify header information - headers already sent by (output started at ...)

我完全理解為什么 PHP 會抱怨這個.問題是為什么 CakePHP 會抱怨,我該怎么辦?

I fully understand why PHP complains about this. The question is why does CakePHP complain and what can I do about it?

需要注意的是,CakePHP 2.x 允許這樣做.

It should be noted that CakePHP 2.x allowed this.

推薦答案

控制器不應該回顯數據!回顯數據會導致各種問題,從測試環境無法識別數據,到無法發送header,甚至數據被截斷.

Controllers should never echo data! Echoing data can lead to all kinds of problems, from the data not being recognized in the test environment, to headers not being able to be sent, and even data being cut off.

那樣做在 CakePHP 2.x 中已經是錯誤的了,盡管它可能在某些甚至大多數情況下都有效.隨著新 HTTP 堆棧的引入,CakePHP 現在在回顯響應之前顯式檢查發送的標頭,并相應地觸發錯誤.

Doing it that way was already wrong in CakePHP 2.x, even though it might have worked in some, maybe even most situations. With the introduction of the new HTTP stack, CakePHP now explicitly checks for sent headers before echoing the response, and will trigger an error accordingly.

發送自定義輸出的正確方式是配置并返回響應對象,或者使用序列化視圖,在 3.x 中仍然相同.

The proper way to send custom output was to configure and return the response object, or to use serialized views, and it's still the same in 3.x.

引自文檔:

Controller 動作通常使用 Controller::set() 來創建一個上下文,View 用它來渲染視圖層.由于 CakePHP 使用的約定,您不需要手動創建和呈現視圖.相反,一旦控制器操作完成,CakePHP 將處理渲染和交付視圖.

Controller actions generally use Controller::set() to create a context that View uses to render the view layer. Because of the conventions that CakePHP uses, you don’t need to create and render the view manually. Instead, once a controller action has completed, CakePHP will handle rendering and delivering the View.

如果出于某種原因您想跳過默認行為,您可以從具有完全創建響應的操作中返回一個 CakeNetworkResponse 對象.

If for some reason you’d like to skip the default behavior, you can return a CakeNetworkResponse object from the action with the fully created response.

* 從 3.4 開始,這將是 CakeHttpResponse

食譜>控制器控制器操作

$content = json_encode(['method' => __METHOD__, 'class' => get_called_class()]);

$this->response = $this->response->withStringBody($content);
$this->response = $this->response->withType('json');
// ...

return $this->response;

PSR-7 兼容接口使用不可變方法,因此使用了 withStringBody()withType() 的返回值.在 CakePHP 中3.4.3、withStringBody()不可用,直接寫入body流即可,不會改變響應對象的狀態:

The PSR-7 compliant interface uses immutable methods, hence the utilization of the return value of withStringBody() and withType(). In CakePHP < 3.4.3, withStringBody() is not available, and you can instead directly write to the body stream, which will not change the state of the response object:

$this->response->getBody()->write($content);

使用已棄用的界面

$content = json_encode(['method' => __METHOD__, 'class' => get_called_class()]);

$this->response->body($content);
$this->response->type('json');
// ...

return $this->response;

使用序列化視圖

$content = ['method' => __METHOD__, 'class' => get_called_class()];

$this->set('content', $content);
$this->set('_serialize', 'content');

這還需要使用請求處理程序組件,并啟用擴展解析和使用附加了 .json 的相應 URL,或者使用 application/json 發送正確的請求代碼> 接受標題.

This requires to also use the request handler component, and to enable extensing parsing and using correponsing URLs with .json appended, or to send a proper request with a application/json accept header.

  • 食譜>控制器控制器操作
  • 食譜>請求 &響應對象 >設置正文
  • 食譜>視圖 >JSON 和 XML 視圖
  • PHP FIG 標準 >PSR-7 HTTP 消息接口

這篇關于如何使用 CakePHP 3.4 輸出自定義 HTTP 正文內容?回聲導致“無法發出標頭"錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 国产视频二区在线观看 | 日韩精品一区二区三区中文在线 | 欧美嘿咻 | 国产一区二区三区四区区 | 欧美国产一区二区 | 男女免费视频网站 | 一级毛片,一级毛片 | 亚洲人成人一区二区在线观看 | 亚洲精品乱码久久久久久蜜桃91 | 精品免费视频 | 欧美亚洲综合久久 | 成年人黄色小视频 | 午夜一区二区三区在线观看 | 欧美在线视频免费 | 欧美极品在线播放 | 欧美日韩免费 | 亚洲国产精品久久 | 国产福利一区二区 | 亚洲精品在线看 | 99热热| 麻豆视频国产在线观看 | 91久久网站 | 91视频大全 | 国产成人精品久久二区二区91 | 国产成人精品午夜视频免费 | 色视频网站在线观看 | 每日更新av| 中国xxxx性xxxx产国 | 99re国产精品| 理论片免费在线观看 | 在线观看日韩精品视频 | 日日日操 | 我要看黄色录像一级片 | 精品日韩一区二区三区av动图 | 不卡在线视频 | 国产成人99久久亚洲综合精品 | 欧美日韩亚洲一区 | 欧美精品久久久久 | 91久久久久 | 91一区二区| 天天综合网7799精品 |