0

is it possible to use a different response header in every route?

You can see my sourcecode here:

  1. Indexfile:

https://github.com/Damian89/slim-filecaching/blob/master/index.php

  1. Caching Middleware:

https://github.com/Damian89/slim-filecaching/blob/master/Caching.slim.php

I have two simple routes. I like to use caching only in the first case, not the second one - but it's not working.

If you place the following code directly above the first route it works:

$app->response->headers->set( PageCaching::SLIM_PAGE_CACHER, PageCaching::CACHE_ENABLED);

But in this case both routes are cached.

Any ideas?

Thank you!

1 Answer 1

1

To me it looks like your caching logic in middleware is reversed. In /no-cache/ route you set the SLIM_PAGE_CACHER header to CACHE_DISABLED whose value is 1.

In middleware you compare the SLIM_PAGE_CACHER header to CACHE_ENABLED whose value is 0. This becomes:

$this->skip_caching (1 == 0); /* This evaluates to false */

This means skip_caching is false and the page will be cached.

In general it is good idea to avoid double negatives in code. They make code hard to read and understand. It is also good idea to follow conventions and assume 1 === true and 0 === false, not the vice versa.

5
  • You are right, but that was an old version of this file. I added the new one. Still - its not the problem. Setting new header-values does not work within routes. Apr 10, 2015 at 6:10
  • Just tested and $app->response->headers->set("X-Test", "foo") works ok. There is logic error somewhere in your code. Apr 10, 2015 at 17:42
  • Damn... it works for me too, but only outside the route -like: $app->add( new \PageCaching() ); $app->response->headers->set( PageCaching::SLIM_PAGE_CACHER, PageCaching::CACHE_ENABLED); In this case, the second route ("/no-cache/" has still CACHE_ENABLED, despite I defined within the route CACHE_DISABLED. Apr 10, 2015 at 18:29
  • Works inside route too. Apr 11, 2015 at 4:38
  • mhm... you are right, its possible, but not passed into my middleware! Apr 11, 2015 at 21:05

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.