Change admin layout in CakePHP

后端 未结 4 1411
旧时难觅i
旧时难觅i 2021-02-15 09:17

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/:c         


        
相关标签:
4条回答
  • 2021-02-15 09:58

    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'));
    ?>
    
    0 讨论(0)
  • 2021-02-15 10:03

    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 讨论(0)
  • 2021-02-15 10:04

    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)

    0 讨论(0)
  • 2021-02-15 10:09

    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 讨论(0)
提交回复
热议问题