Detect the rendering of an error page

Published on and tagged with cakephp  controller  view

Yesterday, I ran into an endless loop in NoseRub while trying to run the migrations to set up the necessary tables. After debugging a while I found the cause for the endless loop: in the beforeRender() method of the AppController we create a model, and as there were no tables at that time, this failed and caused a “missing table” error. Usually, the corresponding error page is then rendered. And that’s it. But not in this case, due to a “feature” I was not aware of: before an error page is rendered, the beforeRender() method of the AppController is called. In this case, it caused again a “missing table” error, and so you have the loop ;-)

To prevent this effect, we have to detect the type of view which is rendered. The only way I found to accomplish this, is to check the viewPath, as shown in the following snippet:

// in app/app_controller.php
public function beforeRender() {
    if ($this->viewPath != 'errors') {
        // no error page
    }
}

It is probably rather rare you have to use this, but maybe it is useful for some.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License