Quick and dirty 'Down for Maintenance' page with CakePHP
Jul 25 2008
Websites need maintenance, and sometimes that maintenance requires the site to go down for a little bit as you tweak the database, add additional capacity or make large changes to the application code. In these circumstances you cannot afford to have users mucking about on your application. However, you might need access to ensure all your amazing upgrades go smoothly or perhaps do the upgrading inside the application itself. I’ve come up with a quick and dirty solution that has served me well in the past. It requires one additional config setting and a little bit more logic in your AppController.
Light switch
I like to keep my off switch as a Configure var. This makes it easy to use and congruent with other settings in core. I usually add the following to my app/config/bootstrap.php.
- Configure::write('App.maintenance', false);
Now I have an easy way to switch off the application, just change false to true and transfer the file to the server.
Lights out
In my applications I like my ‘root’ account to continue having access to the application as I make changes. As a rule my ‘root’ account is always id 1 in the users table. This has other benefits as well. I can easily check and override any permission settings by comparing the user.id in the session to 1. To accomplish a shutdown I simply raises a cakeError when the Configure setting is true and the logged in user is not id 1, effectively locking every page for every user unless they are the ‘root’ user.
In AppController::beforeFilter() I add the following.
Show Plain Text- if (Configure::read('App.maintenance')) {
- if ($this->Auth->user('id') !== 1) {
- $message = __('We are currently working on the application, please check back later.', true);
- 'name' => __('Application down for maintenance', true),
- 'message' => $message,
- 'title' => __('Maintenance', true),
- 'base' => $this->base,
- 'url' => $this->here)
- ));
- }
- }
I find this to be a quick and easy way that doesn’t require a lot of work, but is effective in shutting down an application for a short period of time. When active, every url draws the same page, and prevents any other logic from executing.
Does anyone else have an even easier, and or better way to do this?
Search
Categories
Recent Posts
- Using bindModel to get to deep relations
- New home, new sideproject
- Using bitmasks to indicate status
- AclExtras Shell
- Getting a new Oven, Migrating from CakePHP 1.1 to 1.2
- Code Completion for CakePHP in Eclipse
- CakePHP RC3 released and CakePHP 1.1 new release
- Book Review: CakePHP Application Development
- Providing Contextual Form Help with Mootools
- Creating gracefully degrading javascript and enabling progressive enhancement
Comments
Marc Grabanski on 25/7/08
Nice tip!
Martin Bavio on 25/7/08
Super useful tip. Thanks!
David Persson on 27/7/08
I really don’t know but maybe something like a 503 would be better suited?!? $this->header(‘HTTP/1.1 503 Service Temporarily Unavailable’); $this->header(‘Status: 503 Service Temporarily Unavailable’); $this->header(‘Retry-After: 7200’); // 2 hours
On the other hand you’re a right when you send a Forbidden because it’s (temporarly) forbidden to access that page…
By the way do you know how I can control which status header cake sends when you call $this->cakeError?
As far as I can see the ‘code’ parameter does not set the status header.
Frank on 30/7/08
Hi Mark,
Just thought I’d mention I’ve been having some trouble with your RSS feed in that it keeps downloading articles I already have…
daniel on 14/9/08
Hello, please check this (slightly outdated) post:
http://blog.devayd.com/2008/05/maintenance-module/
we have done something similar, but adding controller/view for a better apperance and password/cookies check so this can be used for testing while the site is not available to regular users.
you can grab the latest code from here: http://anon:anon@websvn.devayd.com/wsvn/public/modules/maintenance/#_modules_maintenance_
regards
daniel
Have Something to say?