zen of coding

Give all of your error messages a different layout

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 () {

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.

  • Matthew Harris

    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.

  • Pingback: CakePHP Digest #13 | PseudoCoder.com()

  • Abu Zaid

    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 ;)

  • Abu Zaid

    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';


  • @Abu Zaid

    Nice, thanks for sharing…

  • Sindrome

    Great tip ! Thanks !

  • @Sindrome

    Glad it helped ;)

  • Pingback: Dica Rápida – CakePHP – Personalizando as páginas de erros « Cauan Cabral – Geek()

  • thanks for sharing it! cakephp documentation sucks mostly and your blog already helped very much twice!

  • Pingback: Tweets that mention Give all of your error messages a different layout | nuts and bolts of cakephp -- Topsy.com()

  • 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!

    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

  • Pingback: CakePHP 2.0 change error layout for admin | Richard Willis-Owen()

%d bloggers like this: