CakePHP caching issue when redirecting back to same page

二次信任 提交于 2019-12-10 22:53:52

问题


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') ); 

回答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.




回答2:


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..!



来源:https://stackoverflow.com/questions/27804628/cakephp-caching-issue-when-redirecting-back-to-same-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!