3

I'm using CakePHP 2.6

I'm having a problem when I redirect back to the same view from where the request was made. The view seems to be cached, so whatever changes were made during the request are not shown until the page is refreshed again.

This means:

  1. the user can't see the changes just made.
  2. Flash messages are shown on the following view (which is bad).

Why is this happening?

Things I've checked:

  • My PHP environment has caching turned off
  • My CakePHP configurations are the defaults (see below).
  • Caching should be disabled because I'm in debug mode: Configure::write('debug', 2);
  • I'm testing in multiple browsers with and without browser caching enabled.

Configure::write('Session', array( 'defaults' => 'php' ));

Representative example:

    //Inside ListingsController...

    $this->Listing->id = $id;
    if ($this->Listing->save($listing)) {
        $this->Flash->success(__('"%s" is now active.', $listing['Listing']['title']));
    } else {
        $this->Flash->error(__('Problem activating'));
    }
    //this is the original view...
    $this->redirect( array('controller'=>'listings', 'action'=>'mylistings') ); 
emersonthis
  • 32,822
  • 59
  • 210
  • 375
  • In debug mode (with `Configure::write('debug', 2);` in /Config/core.php), do you still get this issue? If yes, it is not a caching issue but something else since caching is disabled in debug mode. – PGBI Jan 06 '15 at 19:56
  • @PGBI Yes. I'm in debug mode and it's still happening. Any ideas what else would cause this? – emersonthis Jan 06 '15 at 20:42
  • Not easy to guess... examples of your code might help figuring out. – PGBI Jan 06 '15 at 21:05
  • @PGBI Sure. Just added an example. – emersonthis Jan 06 '15 at 21:08
  • Hi @SDP, I'm seeing something very similar on my project. Did you manage to figure out what was causing it? – dKen Feb 12 '15 at 12:18
  • @dKen Not really. I'm still curious. – emersonthis Feb 12 '15 at 13:24
  • 1
    @SDP Righto, my ridiculously helpful workmate tracked it down to some very overzealous caching. His notes: "`must-revalidate` only works once the `max-age` has been reached. If the `last-modified` date isn't in the past it will just reply with a 304 and pull from cache again". We added the following three lines at the start of the affected actions, and all seems well with the world! Let me know if this helps: `header('Cache-Control: no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); header('Expires: 0');` – dKen Feb 12 '15 at 14:35
  • Huh. Cool. I had a hunch it was something like that. – emersonthis Feb 12 '15 at 14:41

2 Answers2

1

I have the exact same problem. Did you find the cause to all this? As I was trying to solve the issue I saw this in my response headers

Age 0 
Connection keep-alive 
Date Tue, 21 Apr 2015 08:47:21 GMT 
Server ATS/3.2.4

All the files with a 304 Not Modified Status had Apache Traffic Server (ATS) (from my LAN) which led me to think it is the one causing all this behaviour; I forced the non caching as explained here and I have no more problems as such.

Community
  • 1
  • 1
elha
  • 223
  • 2
  • 11
0

Try this it redirects to the referer

$this->Listing->id = $id;
if ($this->Listing->save($listing)) {
    $this->Flash->success(__('"%s" is now active.', $listing['Listing']['title']));
    // Redirect the referer
    $this->redirect(
        $this->referer()
    );
} else {
    $this->Flash->error(__('Problem activating'));
}

Thanks..!