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

- 2,064
- 1
- 16
- 18
-
2Also, to get this to work, you need to specify the Exception renderer as `AppExceptionRenderer` in the core.php file. – Jon Cairns May 14 '12 at 08:43
-
Thanks @JonathanCairns, I forgot to mention that nuance. Added to code example. – Andrew Kulakov May 14 '12 at 23:49
-
Thanks, this works. If you only want to customize the content of your page and don't need custom logic, it's sufficient to omit the creation of a designated erros controller and redirect to the Pages-Controller in your AppExceptionRenderer instead. – bfncs Sep 10 '12 at 13:51
-
Also you need to make sure the Renderer knows about your ErrorsController controller. Override _getController in AppExceptionRenderer. http://book.cakephp.org/2.0/en/development/exceptions.html#creating-a-custom-controller-to-handle-exceptions – Antony Harder Apr 19 '14 at 00:30
-
[Another answer](http://stackoverflow.com/a/12354629/216084) on this page only, is an elegant way, and how CakePHP intended it to be. – Apr 30 '15 at 04:48
-
@plenix I tried all you said but still not applied and i got other errors like mising customer and others – Afaf Nov 30 '16 at 14:35
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.

- 10,007
- 4
- 32
- 52
-
Throwing an exceptions is enough to load this error page - `throw new NotFoundException();` – Mihail Velikov Apr 08 '13 at 08:34
-
Relevant links (`ExceptionRenderer`) in [documentation for 2.x](http://book.cakephp.org/2.0/en/development/exceptions.html#exception-renderer) and [documentation for 3.x](http://book.cakephp.org/3.0/en/development/errors.html#exception-renderer) – Apr 30 '15 at 04:44
-
1
If you're only looking to use another layout instead of the default, just add $this->layout = 'your_error_layout';
inside your error400.ctp (or any other error page you create under View/Errors).

- 1,852
- 1
- 13
- 16
Create a layout with name 404 or anything and use in app controller
function _setErrorLayout() {
if ($this->name == 'CakeError') {
$this->layout = '404';
}
}
function beforeRender () {
$this->_setErrorLayout();
}

- 1,481
- 1
- 13
- 29

- 163
- 5
- 12
The accepted answer is not the best option because they redirect the url of your browser to http://example.com.br/error/error404
and the user can't follow what page he inputed to generate this error.
The better way to handle this situation is edit file on View/Errors/error400.ctp
, so when you input a not found url like http:example.com/crazy-wrong-url
, the browser will keep this url but render the content of the error400.ctp
file that you edit.
If you want change the layout that the view will be rendered you can type this in your view <?php $this->layout = 'error'; ?>

- 1,476
- 5
- 24
- 45
You can create CustomErrorPages
or Exeptions
by simply creating a class
of your error your going to show. This class needs to extend CakeExeption
. Then build your exeptionlogic and your set. Now you can just throw new <YourExeptionClass>()
and it will display an error.
Documentation: CakeExceptions

- 4,768
- 1
- 37
- 64

- 55
- 4
-
1But when would I throw that error for a 404? I mean how does one customise the app 404 page now? – BadHorsie Apr 13 '12 at 16:41
Had discovered today that is possible to get excpetion code in ctp file using $error->getCode()
(in CakePHP 2.x, at least).
The $error
is were Cake puts the exception object...
Now you should be able to change the content of your view with an if/else
conditional block, based in this value.