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