<p>If you're running Craft CMS <a href="https://craftcms.com/changelog#build2783" rel="nofollow">2.6.2783</a>, you can pass in an <code>'expires'</code> key to the optional <code>$options</code> 2nd parameter of <a href="https://craftcms.com/classreference/controllers/BaseController#returnJson-detail" rel="nofollow"><code>returnJson</code></a> 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.</p>

CC BY-SA 3.0
2016-05-02T13:09:44.087Z
Thanks Brad! Just saw this in the 2.6.2783 update: https://craftcms.com/classreference/controllers/BaseController#returnJson-detail
Aaron Berkowitz
CC BY-SA 3.0
2016-04-21T18:04:40.873Z
2016-04-30T04:32:43.663Z
2016-04-30T04:32:43.663Z
Brad Bell
<p>From a custom plugin controller, I am returning data as json using <a href="https://craftcms.com/classreference/controllers/BaseController#returnJson-detail" rel="nofollow"><code>$this-&gt;returnJson($data)</code></a>. I need to be able to set the Cache-Control headers for my forward cache proxy to handle caching correctly. </p>

<p>I tried using <a href="https://craftcms.com/classreference/helpers/HeaderHelper#setExpires-detail" rel="nofollow"><code>HeaderHelper::setExpires(300)</code></a>, but <a href="https://craftcms.com/classreference/helpers/JsonHelper#sendJsonHeaders-detail" rel="nofollow"><code>JsonHelper::sendJsonHeaders()</code></a>, which is called by <a href="https://craftcms.com/classreference/controllers/BaseController#returnJson-detail" rel="nofollow"><code>$this-&gt;returnJson()</code></a> from the controller, overrides <code>setExpires</code> by calling <a href="https://craftcms.com/classreference/helpers/HeaderHelper#setNoCache-detail" rel="nofollow"><code>HeaderHelper::setNoCache()</code></a>. </p>

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

<pre><code>HeaderHelper::setExpires(300);
HeaderHelper::setContentTypeByExtension('json');
ob_start();
echo JsonHelper::encode($return);
craft()-&gt;end();
</code></pre>

<p>but I'd rather use the super convenient <code>returnJson</code> function.</p>

<plugin-development><cache><json>
Using setExpires on returnJson
# Using setExpires on returnJson

(Asked by: username_0 on 2016-04-21)

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.

**Tags:** plugin-development, cache, json


---

## 1 answers

### Answer by username_1 on 2016-04-21. Score: 3 | Accepted answer

If you're running Craft CMS [2.6.2783](https://craftcms.com/changelog#build2783), you can pass in an `'expires'` key to the optional `$options` 2nd parameter of [`returnJson`](https://craftcms.com/classreference/controllers/BaseController#returnJson-detail) 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.

> **Comments:**
> **username_0**: Thanks Brad! Just saw this in the 2.6.2783 update: https://craftcms.com/classreference/controllers/BaseController#returnJson-detail  

---

