Making CakePHP 404 Pages

If you’re after a way to customise the look of your 404 pages then simply change the “/app/views/errors/error404.ctp” page. However if you were like me, and didn’t want your 404 to simply be a static page, rather redirect the user or press the red button for apocalypse to commence, then you have options!
Firstly you need to overwrite the parent error404 function, which the geniuses over at CakePHP headquarters have programmed so this is as easy as pie. Simply make an app_error.php file in the app directory of your file structure. This like app_controller & app_model allows you to develop upon or get access to the parent functions. We will overwrite the default error404 function. With the file open dump in this code:

1
2
3
4
5
6
7
8
9
10
<?php
 
class AppError extends ErrorHandler {
function error404($params) {
$this->controller->redirect(array('controller'=>'pages', 'action'=>'home'));
parent::error404($params);
}
}
 
?>

What this will do is redirect the visitor to the home page, of the pages controller, which happens to be my primary landing page. I feel this is much more useful than simply displaying a static 404 page.