Posted on 4/7/06 by Felix Geisendörfer
The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.
Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.
I often have the problem, that when I'm moving my CakePHP app's between different servers, I simply forget to upload the right app/config/database.php and the result is a huge mess of php errors reminding me of my stupidity ; ). So since clients don't like to see such errors and sometimes your database could really be unavailable for some reasons you cannot control, I thought it would be nice to display a well formated error message, stating the problem.
A quick search to the group brought up the general idea of checking the ConnectionManager's Datasource object and the rest was simply a matter of a couple lines of code. Here is what I came up with:
I changed my app/app_controller.php to look like this:
$db =& ConnectionManager::getDataSource('default');
$php_errors = @ob_get_clean();
$this->viewPath = 'errors';
And added this little command to my app/config/bootstrap.php:
I think the code is pretty self explanatory. You only need to create a file app/views/errors/db_connection_failed.thtml and fill it with some information to make things work. If you wonder about the output buffering: If your DEBUG value (in app/config/core.php) is set higher then 0, CakePHP will display php errors because of the failed connection. My approach buffers them, so you can display them nicely formated in your error view if needed.
Note: This approach doesn't set the base/webroot/here etc. variable in your view, so if you run cakephp from within a subfolder your css includes might break. However, this could easily be added to the solution, it's just that I didn't need any fancy styling for my message.
See a screenshot of how I styled the error message.
You can skip to the end and add a comment.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.