My Ride
Started: Friday 24th May 2013 7:19am
Distance: 11.32km
Duration: 00:28:08
Rest Time: 00:08:46
Climb: 92m
Max Speed: 45.72kmph
Average Speed: 24.14kmphInstagrams
-
Recent Posts
Recent Comments
- Arie on Tag Time: CakePHP Tag Plugin
- Paul on Sign Me Up A CakePHP User Registration Plugin
- Paul on Sign Me Up A CakePHP User Registration Plugin
- Watch The Big Bang Theory season 6 episode 13 on On My Tv: With Trakt.tv
- veloura et bellagenix on Ultimate Guestbook Tutorial: How to build a Guestbook with a honeypot, error checking, IP banning, pagination, e-mail notification and smilies with PHP and mySQL
Archives
- February 2013
- December 2012
- September 2012
- July 2012
- January 2012
- September 2011
- August 2011
- February 2011
- January 2011
- November 2010
- October 2010
- August 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
Categories
Meta
Making CakePHP 404 Pages
August 21, 2009,
10,241 views
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:
<?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.






9 Comments
Which version of Cake are you using? Since template files are called .ctp and not .thtml
This is true! .thtml is depreciated
Thank you!
Been looking for this all over the inter webs, why no one has posted this before is beyond me. Maybe I just haven’t been searching right?
Anyone on the IRC change just tells me to edit /app/views/errors/error404.ctp then stops responding. Yeah, thanks, real helpful guys!
Haha, well i only posted it a few hours ago!
I guess google is on my side on this one!
Remember, views arent where the logic should be, it should be in the controllers
I guess they were telling you where to edit the static 404 messages, not that you can do anything special with that!
Firstly, great little tip, highly useful as I can allow my clients to manage their own Error 404 pages from within the CMS I created.
A few suggested amends to the overriding error function.
1. You need to send a http header to tell the server there was a 404 error otherwise you cannot diagnose when traffic is being sent to wrong pages in your site (I can track 404 errors in AwStats for instance and create pages or url rewrites to make sure I am not losing traffic).
2. The redirect effectively ends the function so there is no need to call parent::error404($params);
controller->redirect(array(‘controller’=>’pages’, ‘action’=>’home’));
}
}
?>
Try again
class AppError extends ErrorHandler {
function error404($params) {
header(“HTTP/1.0 404 Not Found”);
$this->controller->redirect(array(‘controller’=>’pages’, ‘action’=>’error-404′));
}
}
Thanks man for the tip … may b Paul Gardner is logical too
Thanks for the comment Dinesh & Paul. Paul indeed has some good points. The main one i think is that the call to the parent is totally useless because the redirect call halts the method. The other tip from Paul however I don’t 100% agree with. Rendering out the page as a 404 is a bit useless to the end user. Instead a redirect to a more useful page is a much better solution in terms of usability.
Perhaps you could have a dialogue box appear briefly on the redirected page to explain why the user was redirected from the original page they requested. This doesn’t really help in terms of search engines. So perhaps checking the user agent to gather whether they are a searchbot or not and then rendering a 404 depending on who they are. This way you will keep the human users happy and the bots to get rid of their bad links to your site.
Great tip. Did you publish it on Github?
One Trackback
[...] Einfach eine app_error.php im app-Verzeichnis unterbringen und die Standard-Funktion überschreiben [...]