Tuesday, February 8, 2011

How To Customize Main Layout of Error Pages in cakePHP

To customize the contents of each cakePHP errors, you need to create the following pages in this folder /app/views/errors/
- error404.ctp
- missing_action.ctp
- missing_component_class.ctp
- missing_component_file.ctp
- missing_connection.ctp
- missing_controller.ctp
- missing_helper_class.ctp
- missing_helper_file.ctp
- missing_layout.ctp
- missing_model.ctp
- missing_scaffolddb.ctp
- missing_table.ctp
- missing_view.ctp
- private_action.ctp
- scaffold_error.ctp


But cakePHP is using /views/layouts/default.ctp as layout for these error pages like 404, missing controller, etc. To customize it, you need to create a file /app/app_error.php with the following contents:
class AppError extends ErrorHandler {
 
 function _outputMessage($template) {
  $this->controller->layout = 'error_template'; // /app/views/layouts/error_template.ctp
  parent::_outputMessage($template);
 }

}

And create a template file "error_template.ctp" in /app/views/layouts.

Hope this one helps.