CakePHP 2.0 – How to make custom error pages?

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();

Leave a Comment