2011-05-07 3 views
4

나는이 질문을 이미 보았습니다 : Zend Framework how to set headers 그리고 저는 컨트롤러 단위로 헤더를 설정하는 방법을 알고 있습니다.전체 젠드 프레임 워크 응용 프로그램에 대해 콘텐츠 유형 헤더를 설정/변경하는 방법

난 내 설정 파일의 내용 헤더를 설정하려면하고 콘텐츠 유형을 사용하도록 내 모든 응답을 설정 거라고 그러나

$this->getResponse() ->setHeader('Content-type', 'text/html; charset=utf-8')

. 거기에 내가 누락 된 방법/대회에 내장 된 일부가 있습니까? 부트 스트랩에서 무언가를 2 번째로 선택하도록 정착하겠습니다.

이 내 설정에 있습니다

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

내가 모듈과 레이아웃을 사용하고있는 경우의 도움 (이 경우 기본 모듈)

감사합니다.

답변

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'); 
     } 
    } 
} 
+0

처음에는 플러그인 방법이 약간 이상하다고 생각했지만 시도해 본 결과 제대로 작동하는 것으로 보입니다. 감사. –

+0

감사합니다. 도와 드리겠습니다. –

2

헤더가 이미 설정되어 있는지 확인하지 않아도됩니다. setHeader()부터는 대체하지 않고 기본적으로 기존 헤더를 그대로 유지합니다.

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

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

사실, $replace === false 경우, 이미 존재하는 경우에도 젠드 (기본적으로) 헤더를 추가하고 중복 헤더 이름이있을 것이다.

if ($replace) { 
     foreach ($this->_headers as $key => $header) { 
      if ($name == $header['name']) { 
       unset($this->_headers[$key]); 
      } 
     } 
    } 

    $this->_headers[] = array(
     'name' => $name, 
     'value' => $value, 
     'replace' => $replace 
    );