2014-06-14 3 views
2

ZF2에서 헤더를 설정하는 데 문제가 있습니다. 내 코드는 다음과 같습니다.ZF2 - 헤더를 올바르게 설정하는 방법은 무엇입니까?

public function xmlAction() 
{ 
    $headers = new \Zend\Http\Headers(); 
    $headers->clearHeaders(); 
    $headers->addHeaderLine('Content-type', 'application/xml'); 

    echo $file; // xml file content 
    exit; 
} 

하지만 헤더는 여전히 text/html입니다. 적절한 헤더를 다음과 같이 설정할 수 있습니다 :

header("Content-type: application/xml"); 

하지만 젠드 프레임 워크로하고 싶습니다. 왜 위 코드가 작동하지 않습니까?

답변

0

시도 -

public function xmlAction() 
{ 
    $this->getResponse()->getHeaders()->addHeaders(array('Content-type' => 'application/xml')); 

    echo $file; // xml file content 
    exit; 
} 
+0

아니요, 여전히 유감 스럽습니다. – b4rt3kk

12

당신이 ZF2 Response 객체의 헤더를 설정하는 것입니다 일을하지만,이 응답은 을 사용한 적이 이후에 있습니다. 파일을 에코하고 종료하면 ZF2가 응답을 보낼 기회가 없습니다 (헤더 포함).

당신은 당신이 이렇게 할 수있는 파일을 보내 응답을 사용에있는 :

컨트롤러 방법의 응답을 반환의 아이디어는 "단락"라고하며 explained in the manual입니다

public function xmlAction() 
{ 
    $response = $this->getResponse(); 
    $response->getHeaders()->addHeaderLine('Content-Type', 'application/xml'); 
    $response->setContent($file); 

    return $response; 
} 

관련 문제