Slim4 withHeader cache wont overwrite?

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?

Your example works on my machine:

image

I suspect that you might have a middleware problem.

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

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 …

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

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?

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

No. This is the recommend way in Slim.

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

1 Like