zen of coding

  • Dan Sondergaard

    Very useful! Thanks for writing about this, I’ve been wondering for a long time how to do it — and the solution is so damn simple! :D

  • Martin Westin

    Caching is one of these things I have mostly ignored so far. So forgive me if this is a stupid question.

    This problem is with browser cache, right? Allowing the browser to cache any “logged in” page would have the same problem, right?
    I thought Cake have a feature to cache views on the server and that these caches were (or could be made) session-dependent.

  • public function beforeFilter() {
    if (array_key_exists(Configure::read(‘Routeing.admin’), $this->params)) {
    $this->disableCache();
    }
    }

    often does the trick in my apps :)

    • that function only work on my dashboard, and the other page of my application does’nt work, i put this code to my AppController

      public function beforeFilter() {
      if (array(Configure::read(‘Routing.admin’),$this->params)){
      $this->disableCache();

      what should i do with my the other controller.?

      please help me in this case.!

  • @Dan Sondergaard

    No problemo, glad it was helpful.

    @Martin Westin

    Exactly, but this “problem” only happens when the browser caches the page, which for at least any decent browser will be the case unless the proper no-cache headers are sent.

    @Jippi

    Yep, excellent suggestion. Thanks for sharing.

  • mia

    1. function beforeFilter() {
    2. if($this->action == ‘purchase’) {
    3. $this->disableCache();
    4. }
    5. }

    where i should put this source code????help me…

  • @mia

    In your controller. Using the example above, in the controller that has the “purchase” action.

  • Hi, any one have problem with safari 4

    FF and IE both don’t have any problem, when click “back button” and no cache.
    Safari 4 it seem still have cache when click “back button”.

    I’ve tried with this code but does not work. It take me to the previous page and have still show the text on form.

    function beforeFilter()
    {
    //// 1 /////////
    if($this->action == ‘login’) {
    $this->disableCache();
    }
    ///////////////

    //// 2 /////////
    Header( “Last-Modified: ” . gmdate( “D, j M Y H:i:s” ) . ” GMT” );
    Header( “Expires: ” . gmdate( “D, j M Y H:i:s”, time() ) . ” GMT” );
    Header( “Cache-Control: no-store, no-cache, must-revalidate” );
    /////////////
    }

    Any one have problem like me? or Any solution please advise.
    Thanks in advance.

  • watchout

    Thanks for this solution! I’ve been looking all around the net how to disable browser caching in Cake. This solution also works really well if you want to clear a form after refreshing/reloading a page. Normally browsers like Firefox cache the entered data in the form. Telling the browser not to cache the page solves this behavior.

  • teknoid

    @watchout

    Glad it helped.

%d bloggers like this: