Set CakePHP layouts in app_controller

Fed up of writing $this->layout = ‘admin’ in all the admin methods of your controllers? Me too.
I realised that instead you could automatically set them in the beforeFilter() of your app_controller instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
function beforeFilter(){
 
if(isset($this->params['prefix'])) {
//read the admin prefix set in core.php
$admin = Configure::read('Routing.admin');
 
if($this->params['prefix'] == $admin){
 
$this->layout = $admin;
 
}
}
}

If you need to over-ride this, you can just do so as normal in individual methods.