Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2.0 - How to make custom error pages?

I read that the AppError class is now for backwards compatibility and that Exceptions should be used instead. How does one go about creating custom error pages for things like 404 errors, or completely custom errors?

like image 822
BadHorsie Avatar asked Mar 08 '12 15:03

BadHorsie


2 Answers

Try this:

/app/Config/core.php

Exception render need to set as an AppExceptionRender. Example:

Configure::write('Exception', array(         'handler' => 'ErrorHandler::handleException',         'renderer' => 'AppExceptionRenderer',         'log' => true )); 

/app/Controller/ErrorsController.php

class ErrorsController extends AppController {     public $name = 'Errors';      public function beforeFilter() {         parent::beforeFilter();         $this->Auth->allow('error404');     }      public function error404() {         //$this->layout = 'default';     } } 

/app/Lib/Error/AppExceptionRenderer.php

App::uses('ExceptionRenderer', 'Error');  class AppExceptionRenderer extends ExceptionRenderer {      public function notFound($error) {         $this->controller->redirect(array('controller' => 'errors', 'action' => 'error404'));     } } 

/app/View/Errors/error404.ctp

<div class="inner404">     <h2>404 Error - Page Not Found</h2> </div> 

Insert it where you need: throw new NotFoundException();

like image 180
Andrew Kulakov Avatar answered Sep 25 '22 00:09

Andrew Kulakov


To customize the content of a 404-error page and don't need custom logic, simply edit the contents of app/View/Errors/error400.ctp.

This seems not to be documented properly anywhere.

like image 24
bfncs Avatar answered Sep 24 '22 00:09

bfncs