2

From a custom plugin controller, I am returning data as json using $this->returnJson($data). I need to be able to set the Cache-Control headers for my forward cache proxy to handle caching correctly.

I tried using HeaderHelper::setExpires(300), but JsonHelper::sendJsonHeaders(), which is called by $this->returnJson() from the controller, overrides setExpires by calling HeaderHelper::setNoCache().

Does anyone have any ideas for working around this or configuring returnJson? I can do the following in my controller (or create a helper of my own):

HeaderHelper::setExpires(300);
HeaderHelper::setContentTypeByExtension('json');
ob_start();
echo JsonHelper::encode($return);
craft()->end();

but I'd rather use the super convenient returnJson function.

1 Answer 1

3

If you're running Craft CMS 2.6.2783, you can pass in an 'expires' key to the optional $options 2nd parameter of returnJson with the value set to the time in seconds that will be passed to the cache control expiration headers. If you ignore it, it will have the current behavior of not caching at all.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.