除非在同一控制器中写入,否则无法读取cakephp会话

除非在同一控制器中写入,否则无法读取cakephp会话,php,session,cakephp,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'

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

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 supery')控制器不会写入Facebook.accessToken2。

控制器中是否有会话组件

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

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

您的控制器中是否有会话组件

    $components[] = 'Session';
    //or
    public $components = array('Session');
此外,这是仅在该身份验证控制器的登录操作中,还是在其他控制器中?

在您的AppController中:

public $components = array('Session');
function beforeFilter()
{
    $this->Session->write('Facebook.accessToken','This now works superb');
}
在本地控制器中:

function beforeFilter()
{
    parent::beforeFilter();
}
在AppController中:

public $components = array('Session');
function beforeFilter()
{
    $this->Session->write('Facebook.accessToken','This now works superb');
}
在本地控制器中:

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

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


Netbeans和Eclipse可以为您或任何其他具有分步调试功能的php编辑器/ide实现这一点。

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


Netbeans和Eclipse可以为您或任何其他具有分步调试功能的php编辑器/ide完成这项工作。

是的,它位于my
AppController
中。这是我唯一的登录操作(也是目前唯一的控制器)。我应该看看add()操作吗?由于您试图在写入之前读取它,很明显,它是处于该状态的未知变量。另外,$this->Session->read()需要您试图读取的变量的名称。请看一下食谱,好吗。
$this->Session->read()
-“在会话中返回$name处的值。如果$name为null,则整个会话都将返回。”
登录将重定向到
添加
,因此我尝试读取在
登录
中设置的数据,然后在向其添加新数据后再次读取。Add也包含在上面的帖子中。是的,它在my
AppController
中。这是我唯一的登录操作(也是目前唯一的控制器)。我应该看看add()操作吗?由于您试图在写入之前读取它,很明显,它是处于该状态的未知变量。另外,$this->Session->read()需要您试图读取的变量的名称。请看一下食谱,好吗。
$this->Session->read()
-“在会话中返回$name处的值。如果$name为null,则整个会话都将返回。”
登录将重定向到
添加
,因此我尝试读取在
登录
中设置的数据,然后在向其添加新数据后再次读取。上面的帖子中也包括Add。