Issue
Each time I process a post, the code runs thru a function in my own MyController extends AppController
class. That function calls this function to obtain a unique context number. The idea is that this number should be retained from one post to the next (or one run thru the Controller class function to the next). But instead the Configure::check()
always fails and the unique32 is always created anew, so this function always returns 1.
Does anyone know how to make the Configure::write()
get retained from one post to the next?
public function getUnique32() {
if (!Configure::check('unique32')) {
$unique32 = 0;
echo "unique32 starts at 0";
} else {
$unique32 = Configure::read('unique32');
echo "unique32 is $unique32";
}
if (++$unique32 >= 0xffffffff) {
$unique32 = 0;
echo "unique32 is reset to 0";
}
Configure::write('unique32', $unique32);
return $unique32;
}
Solution
Class Configure
does not persist data between page requests. Use instead class Session
.
In CakePHP 3.x, your code should be written as follows:
public function getUnique32() {
$session = $this->request->session();
if (!$session->check('unique32')) {
$unique32 = 0;
echo "unique32 starts at 0";
} else {
$unique32 = $session->read('unique32');
echo "unique32 is $unique32";
}
if (++$unique32 >= 0xffffffff) {
$unique32 = 0;
echo "unique32 is reset to 0";
}
$session->write('unique32', $unique32);
return $unique32;
}
For CakePHP 2.x, you can replace one class for the other in your code.
From the Cookbook 3.x:
Answered By - Inigo Flores
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.