tips

CakePHP Layouts – Leave your default as default

For the past few months I’ve been following a convention of leaving the default.ctp as the default.ctp and putting my main layout in app.ctp, the only reason being is that when you get those missing controller, missing view, missing method errors it looks ugly in any custom default layout.

You can get your application to use the app.ctp layout by specifying

var $layout = 'app';

in your AppController.php. The only problem with this is that cake will still use this layout when an error occurs, to overcome this we create the app_error.php file and override _outputMessage() method.

<?php
class AppError extends ErrorHandler
{
/**
 * Output message
 *
 * @access protected
 */
	function _outputMessage($template) {
		$this->controller->layout = 'default';
		return parent::_outputMessage($template);
	}
}
?>

The above code reverts the layout back to the default.ctp layout whenever an error occurs so you can see those nasty but nice CakePHP errors.