2

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

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。

4

3 回答 3

0

您的控制器中有 Session 组件吗?

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

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

于 2012-09-03T11:56:10.990 回答
0

在您的 AppController 中:

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

在您的本地控制器中:

function beforeFilter()
{
    parent::beforeFilter();
}
于 2012-09-03T12:33:50.253 回答
0

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

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

于 2012-09-03T20:42:16.110 回答