CakePHP 2.0 change error layout for admin

Most sites must have a separate admin area to the main site and many will be using a different layout for the admin area. CakePHP will render errors using the default template so your admin area can look inconsistent and messy if an error has to be raised.

The solution to this follows. I can’t take claim for coming up with this but I’ve adapted from a variety of sources and updated for CakePHP 2.0.

Firstly you need to add some code to /app/Controller/AppController.php, if you don’t have one of these then copy it over from /lib/Cake/Controller/AppController.php. The beforeRender method will check if there is an error then checks if you are using Admin routing. If that is the case the layout is changed:

class AppController extends Controller {

	public $helpers = array('Ulc', 'Html', 'Form', 'Js', 'Session', 'Text');
	public $components = array('Auth', 'Session');

	public function beforeFilter() {
		$this->Auth->allow('*');
	}

	public function beforeRender() {
		$this->_configureErrorLayout();
	}

	public function _configureErrorLayout() {
		if ($this->name == 'CakeError') {
			if ($this->_isAdminMode()) {
				$this->layout = 'admin';
			} else {
				$this->layout = 'default';
			}
		}
	}

	public function _isAdminMode() {
		$adminRoute = Configure::read('Routing.prefixes');
		if (isset($this->params['prefix']) && in_array($this->params['prefix'], $adminRoute)) {
			return true;
		}
		return false;
	}
}

Now in production mode (debug = 0 in core.php) CakePHP will tend to use 2 different error views so it may be worth creating your own customised views.
Copy error400.ctp and error500.ctp from lib/Cake/Views/Errors into app/Views/Errors.
If there is anything you want to display differently in the admin version you can use:

if ($this->layout == 'admin') {}

to detect that you are in the admin area and then hide unnecessary markup that may need to be shown to regular site visitors.

With help from: http://nuts-and-bolts-of-cakephp.com/2009/04/30/give-all-of-your-error-messages-a-different-layout/ and http://bin.cakephp.org/saved/50047

8 Replies to “CakePHP 2.0 change error layout for admin”

  1. Thanks for the tutorial, is very good.

    I have a problem that I can not solve. I need to do the same, but using layout themes.

    All my application uses themes. Can you think of anything?

  2. Hi,
    Richard thanks for the tutorial.
    I have problem when I type the code below in my AppController site:
    / / $ this-> Auth-> allow (‘index’, ‘view’);
    $ this-> set (‘logged_in’, $ this-> Auth-> loggedin ());
    $ this-> set (‘current_user’, $ this-> Auth-> user ());

    / / If its the administrator / manager – change the layout
    if (isset ($ this-> params [‘prefix’]) && $ this-> params [‘prefix’] == ‘admin’) {
    $ this-> layout = ‘admin’;
    Else {}
    $ this-> layout = ‘user’;
    }
    Any help

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.