0

我是 cakephp 的新手,但我到处都在寻找答案,尤其是食谱。

我正在尝试从 cakebook 教程中制作我的“教程博客”的前端和后端。/View/Layouts我在名为admin.ctp, author.ctp,的 ( ) 中制作了不同的布局default.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,不知道为什么

4

1 回答 1

0

(在问题编辑中回答。转换为社区 wiki 答案。请参阅没有答案的问题,但问题在评论中解决(或在聊天中扩展)

OP写道:

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

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

<?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'));
}?>
于 2015-02-04T16:31:36.617 回答