0

I'm trying to attach response headers (like "Pragma: no-cache") to the response for frontend pages.

If I'm the one instantiating the response object, I could just call header method on it as described in this October documentation.

But, to do that from a component, I need to return the response object from onRun method of the component, and it will terminate the layout lifecycle.

I thought about creating middleware to do this, but is there any easier way to add response headers for frontend pages without terminating the layout lifecycle?

kanji
  • 729
  • 9
  • 14

1 Answers1

0

I found a way seems to be the proper way.

Hook the cms.page.display event and create response based on the passed result and your additional response header.

    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);
        });
    }

Please see here for detail about october's event handling.

kanji
  • 729
  • 9
  • 14