【问题标题】:beforeFilter() not being called?beforeFilter() 没有被调用?
【发布时间】:2011-10-20 16:10:12
【问题描述】:

我添加了一个函数来检查我的 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());
    }

}

请帮帮我,这让我发疯了。

【问题讨论】:

  • 你使用 beforeFilter() 有什么原因吗?因为它不会在错误时被调用,因此可能会以错误的布局结束......如果你想避免这种情况,请使用 beforeRender()。

标签: php cakephp cakephp-1.3


【解决方案1】:

我忘了调用基类'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());
    }

}

【讨论】:

  • 那真是无价之宝! (无王子?)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 2018-11-25
  • 2019-03-31
  • 2012-12-16
  • 2015-03-28
相关资源
最近更新 更多