By default CakePHP will use the default.ctp layout to display any error messages.
In some cases this may not be desirable as your default.ctp might contain navigational and design elements, CSS and JS files, which really take away from the way the error message is displayed.
Usually an error message should be displayed in a clean and simple layout that only focuses on the error itself.
Thankfully, the remedy for this is pretty simple…
First, create a new layout and call it error.ctp (place in the app/views/layouts, of course).
Then add this to your App Controller:
function beforeRender () {
$this->_setErrorLayout();
}
function _setErrorLayout() {
if($this->name == 'CakeError') {
$this->layout = 'error';
}
}
That’s all folks.
P.S. Note, this is different than modifying error message views. For instructions on that, please look at the helpful instructions in the actual error message.
14 Responses to “Give all of your error messages a different layout”
That’s interesting. Aren’t there some cases where ErrorHandler won’t initialize a controller, or won’t invoke every AppController callback?
@Matthew Harris
With debug 0, the debugger is not invoked, but the rest works the same for any debug level. I don’t know of any other cases, please let me know if you find one.
on May 4th, 2009 at 7:45 PM #
[...] teknoid post there wouldn’t be room for any other blogs. I’ll pick there two: “Give all of your error messages a different layout” and “More pondering about [...]
Thank u very much for your tips.
This is what i have been looking for and now i found it after i spent a couple hours searching it in google.
There is one more thing I want to know about it. How i set a different layout (for the error of course) for different user.
Example :
For public error (for user that not authenticated or not log in) i prefer use of “public_error” layout, but for user member (user that log in to member area) i prefer use of “member_error” layout.
Can i implement this in cake ?
btw : Your blog is very usefull information of cake !
@Abu Zaid
If you are using cake’s Auth, you can do a simple if() and then set appropriate layout based on whether the user is logged in or not.
And thanks for the compliment ;)
Great,
i do it like this :
if ($this->name == 'CakeError') { if ($this->Auth->user('status')=='member') { $this->layout = 'member'; } else if ($this->Auth->user('status')=='admin') { $this->layout = 'admin'; } else $this->layout = 'default'; }THanks
@Abu Zaid
Nice, thanks for sharing…
Great tip ! Thanks !
@Sindrome
Glad it helped ;)
on January 28th, 2010 at 4:46 PM #
[...] Pronto, agora é só personalizar o arquivo errors.ctp da forma que achar melhor =] Dica retirada do Teknoid [...]
thanks for sharing it! cakephp documentation sucks mostly and your blog already helped very much twice!
on November 19th, 2010 at 6:58 PM #
[...] This post was mentioned on Twitter by Benjamin Dos Santos, Pablo Terradillos. Pablo Terradillos said: http://tinyurl.com/cakerrorlayout // Give all of your error messages a different layout #CakePHP [...]
Some errors are raised before your Controller::beforeRender() method is called (e.g. when a table is missing).
You are safer if you define your own AppError class and override the _outputMessage() method:
# app/app_error.php
class AppError extends ErrorHandler {
function _outputMessage($template) {
$this->controller->layout = ‘error’; // Create a app/views/layouts/error.ctp file!
parent::_outputMessage($template);
}
}
This way you can always display *only* the error message… and that’s what you want to in 99.99% of the cases, don’t you? :-)
Thank you, that was so helpful