【问题标题】:How to attach response headers from components in OctoberCMS?如何在 OctoberCMS 中附加来自组件的响应标头?
【发布时间】:2019-01-07 14:37:18
【问题描述】:

我正在尝试将响应标头(例如“Pragma: no-cache”)附加到前端页面的响应中。

如果我是实例化响应对象的人,我可以在其上调用header 方法,如October documentation 中所述。

但是,要从组件中执行此操作,我需要从组件的 onRun 方法返回响应对象,它将终止 the layout lifecycle

我考虑过创建中间件来执行此操作,但有没有更简单的方法可以在不终止布局生命周期的情况下为前端页面添加响应标头?

【问题讨论】:

    标签: octobercms octobercms-plugins


    【解决方案1】:

    我发现一种方法似乎是正确的方法。

    挂钩cms.page.display 事件并根据传递的结果和您的附加响应标头创建响应。

        public function myComponentMethod()
        {
            Event::listen('cms.page.display', function ($controller, $url, $page, $result) {
                $headers = [
                    // Headers you want to set
                    'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0',
                    'Pragma' => 'no-cache',
                ];
                return Response::make($result, $controller->getStatusCode(), $headers);
            });
        }
    

    有关 october 事件处理的详细信息,请参阅here

    【讨论】:

      猜你喜欢
      • 2020-02-24
      • 2021-12-28
      • 1970-01-01
      • 2015-06-22
      • 2019-03-27
      • 2019-10-26
      • 2020-11-26
      • 1970-01-01
      • 2016-02-23
      相关资源
      最近更新 更多