简体   繁体   English

在CakePHP中更改管理布局

[英]Change admin layout in CakePHP

I am working in cakephp, and I have the following two lines in my /app/config/routes.php file: 我在cakephp工作,我的/app/config/routes.php文件中有以下两行:

/**
 * ...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 我在/app/views/layouts/admin.ctp上也有一个布局

However, the layout is not changed when I visit admin URLs 但是,当我访问管理URL时,布局不会更改

Create a app/app_controller.php and put this in: 创建一个app/app_controller.php并将其放入:

<?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(); 不要忘记调用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 . 半问题,您不需要定义路由,只需启用Routing.admin配置选项并在app/config/core.php中将其设置为admin (CakePHP 1.2) (CakePHP 1.2)

Add this code in beforeFilter() function in app_controller.php app_controller.php中的beforeFilter()函数中添加此代码

<?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 routes.php中设置layout ='admin'

<?php    
Router::connect('/admin', array('controller' => 'users', 'action' => 'index','add', 'admin' => true,'prefix' => 'admin','layout' => 'admin'));
?>

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 上述方法很好,但如果您想在登录时更改每个页面的布局,可以尝试使用Auth Component进行以下操作

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

For cakephp 3.0 you can set a view variable by calling Auth->user in the beforeRender in AppController. 对于cakephp 3.0,您可以通过在AppController中的beforeRender中调用Auth-> user来设置视图变量。 This is my beforeRender: 这是我的beforeRender:

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

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

For CakePHP 3.X you should edit your src/View/AppView.php file and add the following code to your initialize() method: 对于CakePHP 3.X,您应该编辑src/View/AppView.php文件并将以下代码添加到initialize()方法:

public function initialize()
{
    if ($this->request->getParam('prefix') === 'admin') {
        $this->layout = 'Plugin.layout';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM