Jam*_*ler 4 php zend-framework
我已经看到了这个问题: Zend Framework如何设置标头 ,我知道如何在每个控制器的基础上设置标头.
$this->getResponse()
->setHeader('Content-type', 'text/html; charset=utf-8')
但是我想在我的配置文件中设置content-header并让它设置我的所有响应以使用该内容类型.是否有一些内置的方法/惯例我错过了?我会满足于在引导程序中设置一些东西作为第二个最佳选择.
这是我的配置:
resources.view.doctype = "XHTML1_STRICT"
resources.view.encoding = "UTF-8"
resources.view.contentType = "text/html;charset=utf-8"
Run Code Online (Sandbox Code Playgroud)
如果有任何帮助我会使用模块和布局(在这种情况下是默认模块)
问候.
小智 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');
}
}
}
Run Code Online (Sandbox Code Playgroud)