Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change admin layout in CakePHP

Tags:

php

cakephp

I am working in cakephp, and I have the following two lines in my /app/config/routes.php file:

/**
 * ...and setup admin routing
 */
 Router::connect('/admin/:controller/:action/*', array('action' => null, 'prefix' => 'admin', 'admin' => true, 'layout' => 'admin' ));
/**
 * ...and set the admin default page
 */
 Router::connect('/admin', array('controller' => 'profiles', 'action' => 'index', 'admin' => true, 'layout' => 'admin'));

I also have a layout at /app/views/layouts/admin.ctp

However, the layout is not changed when I visit admin URLs

like image 411
xaav Avatar asked Nov 15 '10 01:11

xaav


3 Answers

For CakePHP 3.X you should edit your src/View/AppView.php file and add the following code to your initialize() method:

public function initialize()
{
    if ($this->request->getParam('prefix') === 'admin') {
        $this->layout = 'Plugin.layout';
    }
}
like image 96
Tomas Gonzalez Avatar answered Oct 28 '22 17:10

Tomas Gonzalez


Create a app/app_controller.php and put this in:

<?php
class AppController extends Controller {

    function beforeFilter() {
        if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
            $this->layout = 'admin';
        } 
    }

}

Don't forget to call parent::beforeFilter(); in your controllers if you use it in other controllers.

Semi related to the question, you don't need the routes defined, you just need to enable Routing.admin config option and set it to admin in the app/config/core.php. (CakePHP 1.2)

like image 25
Marko Avatar answered Oct 28 '22 16:10

Marko


Add this code in beforeFilter() function in app_controller.php

<?php    

class AppController extends Controller {

function beforeFilter() {
    if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
        $this->layout = 'admin';
    } else {
       $this->layout = 'user';  
    } 

    }

}
?>

Set layout='admin' in routes.php

<?php    
Router::connect('/admin', array('controller' => 'users', 'action' => 'index','add', 'admin' => true,'prefix' => 'admin','layout' => 'admin'));
?>
like image 41
nusrat Avatar answered Oct 28 '22 15:10

nusrat