2015-01-13 32 views
1

我想從來沒有在Zend框架的一些我的控制器動作響應2.如何在ZF2中爲單個控制器操作設置自定義標題?

這是一個我說控制器的定義:

'login' => array(
    'type' => 'segment', 
    'options' => array(
     'route' => '/login[/:action][/:id]', 
     'constraints' => array(
      'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
      'id' => '[a-zA-Z0-9]*', 
     ), 
     'defaults' => array(
      'controller' => 'Presentation\Controller\Login', 
      'action'  => 'index', 
     ), 
    ), 
), 

我想沒有任何反應的,這個控制器提供被緩存。我試過像這樣的setHeader:

$this->getResponse() 
    ->setHeader('Cache-Control', 'no-cache, no-store, must-revalidate', true) 
    ->setHeader('Pragma', 'no-cache', true) 
    ->setHeader('Expires', '0', true); 

裏面的動作函數,它不起作用。我還在佈局上設置了正確的標題.pthml

回答

4

您正確的方式。在相關操作中,您只需通過$this->getResponse()->getHeaders()實例Zend\Http\Headers實例。

試試這個:

public function myAction() 
{ 
    $headers = array(
     'Cache-Control' => 'no-cache, no-store, must-revalidate', 
     'Pragma'  => 'no-cache', 
     'Expires'  => false, 
     ); 
    $this->getResponse()->getHeaders()->addHeaders($headers); 
} 
+0

是的,這實際工作和頭被正確添加。謝謝。 – Bogdan

相關問題