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.

6 comments:

palstreet said...

Hello, I am having some problem fixing the functionality ofod my PHP code. It is only in one page that it has some issues.can I post the code here for your look?

James said...

I can appreciate you for sharing such an useful information regarding PHP. PHP is one of the best open source that meets the requirements of web. You can also visit programming forum to learn more about PHP Programming.

kamal said...

Thanks for sharing very useful information.

kamal said...

Thanks for sharing very useful info.

Unknown said...

PHP Programmers are having excellent career grouth and thanks for you are tutorial, All the best.

Unknown said...

Excellent information and nice to see this, Thanks for sharing. PHP Programmers