Zend framework 如何在zend_操作中更改响应标头

Zend framework 如何在zend_操作中更改响应标头,zend-framework,Zend Framework,我想做这样的东西: if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { header('HTTP/1.1 304 Not Modified'); exit; } ...... other code ..... 按以下方式键入: public function imageAction() { $request = $this->getRequest(); $response = $this->getRes

我想做这样的东西:

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
    header('HTTP/1.1 304 Not Modified');
    exit;
}

...... other code .....
按以下方式键入:

public function imageAction()
{
    $request = $this->getRequest();
    $response = $this->getResponse();
    if ($request->getHeader('IF_MODIFIED_SINCE')) {
        $response->setHttpResponseCode(304);
        return;
    }
    var_dump($request->getHeaders());
    $response->setHeader('Last-Modified', gmdate('D, d M Y H:i:s', time()).' GMT');
    var_dump($response->getHeaders());
    echo '11111';
    exit;
希望结果读者一定是这样的

> this: Date: Thu, 07 Jul 2011 09:28:08
> GMT Server: Apache/2.2.15 (Linux/SUSE)
> X-Powered-By: PHP/5.3.7RC2-dev
> Last-Modified: Thu, 07 Jul 2011
> 09:27:48 GMT Content-Length: 7101
> Content-Type: text/html
> 
> 200 OK
但它总是:

> Date: Thu, 07 Jul 2011 09:30:16 GMT
> Server: Apache/2.2.15 (Linux/SUSE)
> X-Powered-By: PHP/5.3.7RC2-dev
> Expires: Thu, 19 Nov 1981 08:52:00 GMT
> Cache-Control: no-store, no-cache,
> must-revalidate, post-check=0,
> pre-check=0 Pragma: no-cache
> Content-Length: 347 Keep-Alive:
> timeout=15, max=99 Connection:
> Keep-Alive Content-Type: text/html
> 
> 200 OK

或者我必须使用zend_cache?

它似乎工作正常。尝试这两个操作来测试。您需要在下面的“
yourdomain
”中设置域名

public function headerAction()
{
    $response = $this->getResponse();
    $request = $this->getRequest();

    if ($request->getHeader('IF-MODIFIED-SINCE')) {
        $response->setHttpResponseCode(304);           

        return;
    }

    $response->setHeader('Pragma', 'public', true);
    $response->setHeader('Cache-control', 'max-age=' . 60*60*24*14, true);
    $response->setHeader('Last_Modified', gmdate('D, d M Y H:i:s', time()).' GMT', true);

    // don't exit or die here or Zend Framework won't send our $response or headers
    // If you don't want to render a script, uncomment the two lines below
    //        $this->_helper->viewRenderer->setNoRender();
    //        $this->_helper->layout->disableLayout();

}

public function clientAction()
{
    $client = new Zend_Http_Client('http://yourdomain/test/header');
    $client->setHeaders('If-Modified-Since', 'Sat, 29 Oct 1994 19:43:31 GMT');
    $response = $client->request();

    Zend_Debug::dump($response->getStatus()); //prints 304
    Zend_Debug::dump($response->getHeaders());

    exit;    
}

相对长度单位。。。根据
缓存控制:无存储,无缓存,必须重新验证,后检查=0,预检查=0,如果修改,浏览器似乎不会发送。Pragma:no Cache
如上所述。我现在了解问题,上次修改设置不正确。我已更新我的答案。问题是您正在退出脚本,而不是发送
$response
谢谢–请确保您的.htaccess文件中没有“Header unset Last Modified”一行