gpt4 book ai didi

php - 如何为整个 Zend Framework 应用程序设置/更改内容类型 header

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:01:44 25 4
gpt4 key购买 nike

我已经看过这个问题了: Zend Framework how to set headers并且我知道如何在每个 Controller 的基础上设置 header 。


$这->getResponse()
->setHeader('内容类型', 'text/html; charset=utf-8')

但是我想在我的配置文件中设置内容 header ,并让它设置我所有的响应以使用该内容类型。是否有一些我错过的内置方法/约定?我将在 Bootstrap 中设置一些东西作为第二个最佳选择。

这是我的配置:

resources.view.doctype = "XHTML1_STRICT"
resources.view.encoding = "UTF-8"
resources.view.contentType = "text/html;charset=utf-8"

如果有任何帮助,我正在使用模块和布局(在本例中为默认模块)

问候。

最佳答案

您可以为此编写一个插件,在尚未设置其他内容类型时自动将内容类型设置为默认值。示例:

class YourApp_Controller_Plugin_ContentType extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopShutdown()
{
$response = $this->getResponse();
$http_headers = $response->getHeaders();

/**
* Check whether the Content-Type header is defined.
*/
$content_type_found = false;
foreach ($http_headers as $_header)
{
if ($_header['name'] === 'Content-Type')
{
$content_type_found = true;
break;
}
}

/**
* When no Content-Type has been set, set the default text/html; charset=utf-8
*/
if (!$content_type_found)
{
$response->setHeader('Content-Type', 'text/html; charset=utf-8');
}
}
}

关于php - 如何为整个 Zend Framework 应用程序设置/更改内容类型 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5922391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com