Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2.0 - beforeFilter() not run on cakeErrorController?

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:

  1. This is a bug in CakePHP's default exception handling,
  2. The behavior is as intended and I just don't understand it, or
  3. 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.

like image 662
eaj Avatar asked Jan 27 '12 23:01

eaj


2 Answers

Put the calls to $this->set() in the beforeRender() callback. That way, they'll always be set even when you throw an exception.

I had the same problem myself with some custom layout variables, as with DebugKit my page would be filled with undeclared variable warnings whenever there was any sort of error. Using beforeRender() instead fixed it.

like image 67
mrlee Avatar answered Nov 16 '22 04:11

mrlee


you can overwrite the CakeErrorController.php by copying it to App/Controller/ and then add parent::beforeFilter() into the constructor.

like image 32
csx Avatar answered Nov 16 '22 02:11

csx