cakephp改变了不同用户的布局

时间:2013-11-05 13:52:28

标签: cakephp layout

我是cakephp的新手,不过我到处寻找答案,特别是食谱。

我正在尝试从cakebook教程制作我的“教程博客”的前端和后端。我在(/View/Layouts)中创建了名为admin.ctpauthor.ctpdefault.ctp的不同布局,并且在控制器中使用了一些代码我可以打赌它会起作用,但不会。

    public function beforeFilter() {
    parent::beforeFilter();

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

}

它总是选择最后一个default.ctp,我不知道为什么

1 个答案:

答案 0 :(得分:0)

(在问题编辑中回答。转换为社区维基回答。请参阅Question with no answers, but issue solved in the comments (or extended in chat)

OP写道:

  

我转而没有在core.php中设置任何前缀,我试图设置它们但没有任何进展,如果有人可以帮我根据我的代码设置一些前缀,我将不胜感激。< / p>      

尽管如此,我解决了编辑控制器的问题,它运行正常。它不专业,我知道,但现在它应该对我有用。

<?php

class ProductsController extends AppController
{
public $helpers= array('Html','Form','Session');
public  $components = array('Session');


public  function index()
{
    if($this->Session->read('Auth.User.role') == 'admin')
    {
        $this->layout = 'admin';
    }
    else if($this->Session->read('Auth.User.role') == 'author')
    {
        $this->layout = 'author';
    }
    else
    {
        $this->layout = 'default';
    }
    $this->set('item',  $this->Product->find('all'));
}?>
相关问题