# Slim4 withHeader cache wont overwrite?

**URL:** <https://discourse.slimframework.com/t/slim4-withheader-cache-wont-overwrite/3794>\
**Category:** Questions\
**Created:** [January 14, 2020, 1:01pm UTC](https://discourse.slimframework.com/t/slim4-withheader-cache-wont-overwrite/3794 "2020-01-14T13:01:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [January 14, 2020, 1:01pm UTC](https://discourse.slimframework.com/t/slim4-withheader-cache-wont-overwrite/3794/1 "2020-01-14T13:01:06Z")

</div>

Hi!  
I am running this code;  
$response = $response-\>withHeader(‘Cache-Control’, ‘public, max-age=86400’);  
$logger-\>info($response-\>getHeaderLine(‘Cache-Control’));  
return $response;

Which seem pretty basic.  
the log prints “slim-app.INFO: public, max-age=96400 [] {“uid”:“8cbf429”}”,  
but yet, curl/postman/firefox/chrome says :

Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0

Which is the default as i understand?  
Why wont it get overwritten?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 14, 2020, 1:42pm UTC](https://discourse.slimframework.com/t/slim4-withheader-cache-wont-overwrite/3794/2 "2020-01-14T13:42:41Z")

</div>

Your example works on my machine:

![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/0/0624c8823d598a9c7a8eeca703d165949c9c9164.png)

I suspect that you might have a middleware problem.

---

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [January 14, 2020, 1:49pm UTC](https://discourse.slimframework.com/t/slim4-withheader-cache-wont-overwrite/3794/3 "2020-01-14T13:49:36Z")

</div>

I wonder what that could be, i am basically just using a slim-skeleton.  
Sucks thats an “its-just-me” issue …

---

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [January 14, 2020, 3:06pm UTC](https://discourse.slimframework.com/t/slim4-withheader-cache-wont-overwrite/3794/4 "2020-01-14T15:06:26Z")

</div>

I found that its this block thats atleast overwriting it:

```
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);

```

But thats the final part of index.php, and i am not sure how to “not overwrite” it …

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 14, 2020, 5:29pm UTC](https://discourse.slimframework.com/t/slim4-withheader-cache-wont-overwrite/3794/5 "2020-01-14T17:29:31Z")

</div>

Slim will invoke the response emitter for you.  
At the end of index.php you should just call `$app->run();`

---

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [January 15, 2020, 7:19am UTC](https://discourse.slimframework.com/t/slim4-withheader-cache-wont-overwrite/3794/6 "2020-01-15T07:19:40Z")

</div>

Alright, removing  
// Run App & Emit Response  
$response = $app-\>handle($request);  
$responseEmitter = new ResponseEmitter();  
$responseEmitter-\>emit($response);

And adding $app-\>run(); solved it, but did i remove anything crucial?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 15, 2020, 7:54am UTC](https://discourse.slimframework.com/t/slim4-withheader-cache-wont-overwrite/3794/7 "2020-01-15T07:54:24Z")

</div>

> And adding $app-\>run(); solved it, but did i remove anything crucial?

No. This is the recommend way in [Slim](http://www.slimframework.com/docs/v4/).

The only exception I know is when you implement integration (http) tests.  
Then you should use `$response = $app->handle($request)` instead of `$app->run();`.

[Example](https://github.com/odan/slim4-skeleton/blob/master/tests/TestCase/HttpTestTrait.php#L79)

---

<div class="post-metadata">

**Author:** ![Pictor13](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/pictor13/32/1684_2.png) [@Pictor13](https://discourse.slimframework.com/u/Pictor13)\
**Post date:** [May 13, 2025, 6:06pm UTC](https://discourse.slimframework.com/t/slim4-withheader-cache-wont-overwrite/3794/8 "2025-05-13T18:06:54Z")

</div>

Also beware: [the skeleton example app sets Cache-Control in ResponseEmitter (hence for **every** response!) to `no-store, no-cache`](https://github.com/slimphp/Slim-Skeleton/blob/main/src/Application/ResponseEmitter/ResponseEmitter.php#L28-L30).  
You might want to remove `Cache-Control` & `Pragma`, in order to get your load-balancer (e.g. Varnish) to cache correctly and improve performances.

If you want to prevent caching on some specific resource you can use `withHeaders('Cache-Control', ... )` on a specific response only for specific routes.

Another alternative is to leave the ResponseEmitter as it is, but use `withoutHeaders('Cache-Control')`, to unset the default headers and allow to use your own.
