doulianxi0587 2016-08-30 07:15
浏览 41
已采纳

在CakePHP中多次提交请求后,使变量变为静态?

I tried to declare a static and public variable in AppController as follows

class AppController extends Controller {


public static $var = 0;

}

And then access the static variable in controller of its subclass.

My CTP file has multiple submit buttons which change the value of the static variable.

eg.

button A -> $var ++  
button B -> $var --

I also tried session and started it in beforefilter function of app controller. Still on multiple submission, the session variable is reset again.

class AppController extends Controller {


public function beforeFilter() {
parent::beforeFilter();
$this->Session->write('var', 0);

}
}

In both cases(either static variable or session), the static variable can only be +1, 0 or -1. It seems the AppController is loaded on refresh of page. What I really want is the static variable can be + or - more than 1 after the buttons are pressed multiple times and the static variable is only reset after close of the browser.

  • 写回答

1条回答 默认 最新

  • drdu53813 2016-09-01 04:54
    关注

    For setting the value when you want, by buttons, you should use post method in some controller action not in beforeFilter().

    example:

    public function foo() {
        if ($this->request->is('post')) {
            // get posted value from $this->request
            $this->Session->write('var', 'value');
        }
    }
    

    then you can read the value in any controller like this:

    $value = $this->Session->read('var');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献