如何在 ZF2 中为单个控制器操作设置自定义标头? [英] How to set custom headers for individual controller actions in ZF2?

查看:23
本文介绍了如何在 ZF2 中为单个控制器操作设置自定义标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想在 Zend 框架 2 中响应我的某些控制器操作.

I want to never the response of some of my controller actions in zend framework 2.

这是我所说的控制器的定义:

This is the definitions of one my said controllers:

'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:

I want none of the responses that this controller supplies to be cached. I tried setHeader like this:

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

在动作函数内部,它不起作用.我还在布局 .pthml

inside the action functions, and it doesn't work. I also set the correct headers on the layout .pthml

推荐答案

您走对了路.你只需要实际的 Zend\Http\Headers 通过 $this->getResponse()->getHeaders() 在相关操作中的实例.

You're on the right way. You just need the actual Zend\Http\Headers instance via $this->getResponse()->getHeaders() inside the related action.

试试这个:

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

这篇关于如何在 ZF2 中为单个控制器操作设置自定义标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆