除非我在同一个控制器中写入,否则不能读取cakephp会话

时间:2012-09-03 11:46:40

标签: php session cakephp

除非我在想要读取的控制器的开头写入,否则我无法读取会话数据。 示例如下:

public function login(){
$this->Session->write('Facebook.accessToken','This now works superb');
    //TODO: Implement check to see if logged in user has the same session user id.
    require_once(APP . 'Vendor' . DS .'facebook.php');
    $facebook = new Facebook(array(
    'appId'  => 'REMOVED',
    'secret' => 'REMOVED'
    ));
    $user = $facebook->getUser();
    try {
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>'http://www.facebook.com/REMOVED'));
        $this->set('loginUrl',$loginUrl);
    }

    if(!isset($loginUrl)){
    // means that we are authenticated, so check for login with user id. If logged in redirect to event-list. If login failed display join form.    
        if($user = $this->User->findById($user_profile['id'])){
            if($this->Auth->login($user['User'])){
                $this->redirect('/users/dash');
            }
        }else{
            $this->Session->write('Facebook.accessToken2',$facebook->getAccessToken());
            $this->redirect(array('controller'=>'users','action'=>'add'));
            }
    }
}


public function add(){
    debug($this->Session->read());
    $this->Session->write('testing','value');
    debug($this->Session->read());
    }

如果我删除$this->Session->write('Facebook.accessToken','This now works superb');,控制器将不会写Facebook.accessToken2。

3 个答案:

答案 0 :(得分:0)

你的控制器中是否有Session组件?

    $components[] = 'Session';
    //or
    public $components = array('Session');

此外,这只是在此身份验证控制器的登录操作中还是在其他控制器中?

答案 1 :(得分:0)

在AppController中:

public $components = array('Session');
function beforeFilter()
{
    $this->Session->write('Facebook.accessToken','This now works superb');
}

在您的本地控制器中:

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

答案 2 :(得分:0)

我建议使用调试器和启用XDebug来逐步执行代码,以确保遵循您期望的代码路径。

Netbeans和Eclipse可以为您或任何其他php编辑器/ ide执行此操作,具有debbuging步进功能。