I don’t get why the variables are not changed in the AppController. I have a subclass of AppController and in the action method, I am changing a variable of AppController. However this does not get reflected in the afterFilter.
This is CakePHP’s AppController
<?php
class AppController extends Controller {
var $xxxx;
function beforeFilter()
{
$this->xxxx = 'this should be changed';
}
function afterFilter()
{
var_dump($this->xxxx);
exit;
}
}
?>
And this is my UsersController
<?php
class UsersController extends AppController {
function view( $id )
{
echo "From the AppController: {$this->xxxx} \n";
$this->xxxx = 'with this';
}
}
?>
This is the output when I run it:
From the AppController: this should be changed
string 'this should be changed' (length=22)
I am expecting this:
From the AppController: this should be changed
string 'with this' (length=9)
Do you know why it behaves this way? Any pointers how to do this correctly?
You should use beforeRender() instead of afterFilter().
Request Life-cycle callbacks will surely help you to achieve the same.
Kindly ask if it not worked for you.