Error handling in cakephp

Error handling in CakePHP is pretty simple and straight forward. While developing in CakePHP with debug value set to > 0 (Configure::write(‘debug’, 1); in app/config/core.php) you might have encountered error messages something similar to this:

CakePHP: the rapid development php framework

Missing Controller

Error: DsfdsfsfsController could not be found.

Error: Create the class DsfdsfsfsController below in file: app\controllers\dsfdsfsfs_controller.php

Notice: If you want to customize this error message, create app\views\errors\missing_controller.ctp

(If you did’t see such error before, just change debug to 1 in app/config/core.php and miss-spell your controller name in the browser’s address bar something similar to http://mysite/controllarname and press enter)

In fact, this page is a view page named missing_controller.ctp inside cake/libs/views/errors directory. It uses default layout which is inside cake/libs/views/layout directory. These views and layouts can be overwritten by placing views and layouts with similar names inside app/views/errors and app/views/layouts directories respectively.

For example, you want to display “We are sorry but this url does not exist” message instead of showing the message above. To make it happen just create a missing_controller.ctp file and place it inside /app/views/errors directory.

(Keep in mind that these custom error messages (missingAction, missingController, missingTable etc.) are displayed only if debug mode is set to greater than 0 (>0). When debug is set to zero (production mode) all error messages are forced to appear within error404.ctp page only. So for the production mode you might need to customize the error404.ctp page only.)

Next step is to create a custom layout for our error message. Just create a new layout default.ctp and place it inside app/views/layouts with $content_for_layout place holder for our error view’s content.

To customize other messages like missing table or missing action etc. just create other related views and place them inside the app/views/errors directory.

Extending CakePHP error handling

While looking for the stuff on CakePHP error handling i found some good articles on the web. First one Error Handling describes the way to create your own custom error handling methods similar to default missingController and missingAction etc. The second article Dealing with errors in CakePHP by teknoid is an extension to the previous one and seems more useful one.

One thought on “Error handling in cakephp

  1. I added the following to the top of the error404() function in my AppError class.

    // When CakePHP’s ‘debug’ is set to 0 for production mode the
    // Cake library ignores the method name you passed to the
    // cakeError() function. Instead it calls this error404()
    // function.
    //
    // To be able to call our own error views in production mode
    // we pass the error name in using the $params argument and
    // then here we check for that parameter name and call the
    // correct method.
    //
    // Example: to call the soapLookupFailed() method call
    // cakeError() via:
    //
    // $error_params = array(‘error_method’ => ‘soapLookupFailed’);
    // $this->cakeError(‘soapLookupFailed’, $error_params);

    if(array_key_exists(‘error_method’, $params)) {
    call_user_func(array($this, $params[‘error_method’]), $params);
    return;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *