I've worked with CakePHP 1.3, but this is my first foray into CakePHP 2.0 and PHP exceptions, so bear with me if my question seems wordy.
In my AppController
's beforeFilter()
I set a couple of variables via $this->set()
for use in my view template.
In my UsersController
I have code that looks roughly like this:
public function beforeFilter() {
parent::beforeFilter();
if (userDeniedAccess()) {
throw new ForbiddenException();
}
}
where the hypothetical function userDeniedAccess()
encapsulates my authorization checks.
This all works fine, and I get the 403 error when I expect it. However, when the exception is thrown, the view variables that were set in AppController::beforeFilter()
are no longer set, resulting in errors from my view template. When the exception is not thrown, the variables are set correctly.
I can code around the missing variables if I must, but I'd really like to know what causes this behavior. Both my UsersController
and the CakeErrorController
extend AppController
. Naturally, I expect that when the exception is thrown and the CakeErrorController
is instantiated, it would generate the same view variables for me.
However, it seems that the Controller::startupProcess()
method (which includes the call to beforeFilter()
) never gets called on CakeErrorController
. As I understand it, this is done by the dispatcher for regular requests, but the life-cycle of the error controller is different.
I've seen descriptions (like this one) of similar behavior in CakePHP 1.3, but of course CakePHP's error handling code has been completely overhauled in 2.0.
So either:
- This is a bug in CakePHP's default exception handling,
- The behavior is as intended and I just don't understand it, or
- I'm going nuts.
I know you can't help with case 3, but if either of the first two apply, I'd appreciate input from someone who knows more than I do.
Thanks!
EDIT: Setting the view variables in beforeRender()
does solve my problem. However, I still wonder whether it is intentional that beforeFilter()
never gets called on CakeErrorController
.