Let’s assume we do not want to show any errors when a non-existing controller is called. The user should simply be redirected to the homepage in such a situation. To accomplish that we have to write a custom error handler.
First, we have to create a file called “error.php” in the “app” directory. We add an “AppError” class to this file which extends the default error handler (cake/libs/error.php) and override the “missingController” function.
class AppError extends ErrorHandler
{
function missingController($params)
{
$this->controller->redirect('/');
}
}
That’s it? Unfortunately, no. This solution works when we use a debug level > 0, but with a debug level of 0 we get a 404 error. We find the reason for that behaviour in the following lines of the default error handler’s constructor:
if (DEBUG > 0 || $method == 'error') {
call_user_func_array(array(&$this, $method), $messages);
} else {
call_user_func_array(array(&$this, 'error404'), $messages);
}
So we have to write our own constructor in our class (fortunately, we can copy almost the entire constructor of the default error handler). Now our AppError class looks like:
class AppError extends ErrorHandler
{
function __construct($method, $messages)
{
static $__previousError = null;
$this->__dispatch =& new Dispatcher();
if ($__previousError != array($method, $messages))
{
$__previousError = array($method, $messages);
if (!class_exists('AppController'))
{
loadController(null);
}
$this->controller =& new AppController();
$this->__dispatch->start($this->controller);
if (method_exists($this->controller, 'apperror'))
{
return $this->controller->appError($method, $messages);
}
}
else
{
$this->controller =& new Controller();
}
call_user_func_array(array(&$this, $method), $messages);
}
function missingController($params)
{
$this->controller->redirect('/');
}
}
Thanks to PhpNut for the hint with the constructor, and to drSwank for asking in the irc.
10 Comments
Thanks for this post. Another option to do this is the following:
class AppController extends Controller
{
function appError($method, $params)
{
$this->controller->redirect(‘/’);
}
}
Nice, might came in handy. Just one thing about the layout: With IE 6.0 the content get’s cut off right at the border, I suppose it’s ment to display some sort of scrollbar?
@Felix: Thanks for the addition. Something learned :)
@Konrad: Hm, I am sorry, but here on wordpress I do not have access to the layout.
Actually, scratch that, I’m not getting a scrollbar in FF (1.5.x) either.
http://img81.imageshack.us/img81/8122/codesnippet1rv.jpg
So in what Browser exactly can you seen the complete Code? :)
@Konrad: Well, I use FF 1.5.x without any problems. Anyway, I posted the code as a snippet on cakeforge: http://cakeforge.org/snippet/detail.php?type=snippet&id=79
@Konrad: you can always select the code and paste in your editor
I’m curious if, after this new Cake update (that takes care of a security vulnerability through the error page), this custom error handler is now flawed as well?
I had been using this (above) for some time, and it worked great. But after recent cake update & patch (as of Aug 17th) my error page breaks… giving Fatal Error .. in app_controller. Doesn’t seen to be able to call PartyAuth->set() (a custom method we had used).
Thanks for your time…!
@morris: Hm, it works fine for me.
Thanks for the custom error handler code, just what I have been searching for.
thanks
@Bompa: I am glad it is useful for you :)
One Trackback/Pingback
[...] cake baker » Writing a custom error handler A very useful error handler for missing controllers. (tags: cakephp controller errorhandler) [...]