CakePHP and Error Page Issues
All right, let's get technical. Here at Merge HQ we use a top-notch framework called CakePHP to build some of our custom applications, but recently I encountered a mystifying issue whilst hacking away on a big project. Cake seems to have an odd habit of ignoring some of your code when it generates error pages (you know, those lovely 404 "file not found" pages we all cherish). In my case, the title of the site and the main menu refused to display, so that's a bit jarring, to say the least.
Fortunately, after a day of sifting through more CakePHP code than I'd prefer, I happened upon a solution. This is a problem that has plagued plenty of developers besides me, and I can only hope they might benefit as well. So what's the scoop? The short version is simply to add the following code to your app_controller.php file:
function __construct() {
parent::__construct();
if ($this->name == 'CakeError') {
$this->constructClasses();
$this->beforeFilter();
}
}This forces Cake to load the components your site uses and run the normal setup code on your site's error pages. The long version of this solution can be read (or fallen asleep to) on my blog. Let it never be said the life of a web developer is just all cherries and ice cream (although admittedly there is an ample supply of both).

Comments
Lifesaver!
Unbelievable how hidden and undocumented this specific case it. I spent a few hours on this last night and got nowhere. I copy/pasted your code in and it just works. Thanks a million!
Post new comment