9

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

5 Answers 5

30

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)

2
  • Thanks! On the Routing.admin config option: 1. That has been changed to routing.prefix 2. I changed it because it did not provide the 'layout' option, but now that is irrelavent 3. Even with the Routing.admin (or prefix)on, I still need the second route for an index page
    – xaav
    Nov 15, 2010 at 4:25
  • your second route is still needed, however i dont believe you need 'layout' => 'admin' all that is doing is adding a param onto your url (if you set pass) that will only be available on that page. the Routing.prefixes does something similar to your first route :)
    – dogmatic69
    Nov 22, 2010 at 0:13
3

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'));
?>
1

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

the approaches above are good but if you are looking to change the layout for every page when logged in you might try the following using Auth Component

function beforeFilter() {
    if ($this->Auth->user()) {
        $this->layout = 'admin';
    }
}
0

For cakephp 3.0 you can set a view variable by calling Auth->user in the beforeRender in AppController. This is my beforeRender:

public function beforeRender(Event $event)
{
    ///...other stuff

    $userRole = $this->Auth->user();
    $this->set('userRole', $userRole['role']);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.