dongleiqiao2107 2014-10-23 12:38
浏览 32
已采纳

拥有authComponent时,sessionFilter()中没有设置会话

I have a problem in my CakePHP application which is that there is no Session object available in my AppController beforeFilter() function, but only when I have Auth Component in it. I have the following code to set the site's language:

public function beforeFilter() {
    $this->_setLanguage();
    $locale = Configure::read('Config.language');
    $path = APP . 'View' . DS . $locale . DS . $this->viewPath;
    if ($locale && file_exists($path)) {
        $this->viewPath = $locale . DS . $this->viewPath;
    }
}

protected function _setLanguage() {
    if (!$this->Session->check('Config.language')) {
        $langLetters = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : 'en';
        $browserLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        switch ($browserLanguage){
            case "en":
                $this->Session->write('Config.language', 'en');
                break;
            case "pt":
                $this->Session->write('Config.language', 'pt');
                break;
            default:
                $this->Session->write('Config.language', 'en');
        }
    }
    Configure::write('Config.language', $this->Session->read('Config.language'));
}

Problem is that, when I add:

public $components = array('Auth');

I get the following error:

Fatal error: Call to a member function check() on a non-object in /home/brasilgameshow/www/teste_credenciamento/app/Controller/AppController.php on line 73

which is the first line of the _setLanguage() function. That means that I simply don't have the Session object anymore when Auth component is present. Could anybody suggest a workaround which wouldn't compromise Auth component's security? I mean, would simply initializing a new session when there is none set be a good solution?

  • 写回答

2条回答 默认 最新

  • dongou4052 2014-10-23 12:59
    关注

    By default if you don't add any components CakePHP would load its default components (Session)

    When you tried to invoke your own components array (with Auth in this case) you should include all other components you may need

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题