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

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Press Releases

Up

October 3, 2011
GREENVILLE, S.C. – Merge, a digital agency in the Upstate, is now a registered Apple Developer in the iOS Developer Program, which allows the...
August 3, 2011
Merge, a leading digital agency in the Upstate, completed a successful second quarter with the June launch of a new website for Orion Air Group, a...
August 3, 2011
In July, Merge launched sites for two Upstate non-profit organizations, Greenville County First Steps and the Julie Valentine Center. ...
July 12, 2011
Merge, a digital agency in the Upstate, is now recognized as a Google Adwords Certified Company, becoming one of only 15 businesses in South...
April 26, 2011
In January, Merge launched a new website for the Center for Development Services (CDS), which now better reflects the valuable services they provide...
January 29, 2011
D&W Fine Pack has retained Merge to develop and execute a strategic, interactive solution to enhance the company's brand and to clearly market...
January 3, 2011
GREENVILLE, S.C. - January 3, 2011 Merge, a leading web development firm in the Upstate, has announced the hire of Eric Evans of Greenwood, SC, as...
December 20, 2010
GREENVILLE, S.C. - December 20, 2010Merge, an Upstate agency providing strategic web solutions, has announced the addition of Meredith Garner as the...
December 15, 2010
GREENVILLE, S.C. - December 15, 2010 Merge, a Greenville-based web development firm that provides comprehensive and strategic web solutions, has...
November 22, 2010
GREENVILLE, S.C. - November 22, 2010 Greenville-based web marketing firm Merge has been selected by TEDxGreenville to provide strategic enhancements...