beforeFilter()没被调用?

时间:2011-10-20 16:10:12

标签: php cakephp cakephp-1.3

我添加了一个函数来检查我的AppController的语言参数

function beforeFilter(){
    if(Configure::read('Config.language') == 'ara'){
         $this->layout = 'rtl-layout';
    } else {
        $this->layout = 'ltr-layout';
    }
}

但它在我的其他控制器中不起作用?

class ImagesController extends AppController {

    var $name = 'Images';
    var $helpers = array('TinyMCE');

    function beforeFilter(){
        if(!isset($this->params['admin'])) {
            $this->Session->setFlash(__('Access denied.', true));
            $this->redirect(array('controller'=>'users','action'=>'login','admin'=>false));
            exit();
        }    
    }

    function index() {
        $this->Image->recursive = 1;
        $this->set('images', $this->paginate());
    }

}

请帮助我,这让我疯狂。

1 个答案:

答案 0 :(得分:6)

我忘记调用基类“beforeFilter(),因为我在ImagesController

中重载了它
class ImagesController extends AppController {

    var $name = 'Images';
    var $helpers = array('TinyMCE');

    function beforeFilter(){
        parent::beforeFilter(); // <-- here
        if(!isset($this->params['admin'])) {
            $this->Session->setFlash(__('Access denied.', true));
            $this->redirect(array('controller'=>'users','action'=>'login','admin'=>false));
            exit();
        }    
    }

    function index() {
        $this->Image->recursive = 1;
        $this->set('images', $this->paginate());
    }

}