CodeIgniter-将类实例存储在CI会话中


CodeIgniter - Store class instance in CI session

最近,我一直想在我的CodeIgniter项目中实现用于PHP的Facebook SDK。Facebook sdk可能很慢,这就是为什么我想存储一个包含收集的fb&fb用户会话中的用户详细信息。

当我存储类实例时,只要我不刷新或重定向,我以后就可以使用它。当我重定向时,值是未设置的。。。

我读过关于使用serialize和unserialize(用于全局PHP会话)的文章,但在这种情况下没有帮助。

这是fb类:

class FBClass {
    protected $FB = false;
    protected $FBUser = false;
    protected $FBUserProfile = false;
    protected $FBUserEmail = false;
    protected $loggedIn = false;
    protected $config = array(
        'appId' => '007',
        'secret' => '911',
        'cookie' => true,
        'fileUpload' => false,
        'allowSignedRequest' => false
    );
    protected $params = array(
        'client_id' => '007',
        'scope' => 'email'
    );
    function __construct() {
        if (!$this->FB) {
            $this->FB = new Facebook($this->config);
        }
    }
    public function isLoggedIn() {
        return $this->loggedIn;
    }
    public function getLoginUrl() {
        $this->FB->getLoginUrl($this->params); 
    }
    public function getUser() {
        if (!$this->FBUser) {
            $this->FBUser = $this->FB->getUser();
        }
        return $this->FBUser;
    }
    public function getUserProfile() {
        if (!$this->FBUserProfile) {
            $this->FBUserProfile = $this->FB->api('/me','GET');
        }
        if ($this->FBUserProfile && !$this->FBUserEmail) {
            $emailArray = $this->FB->api('/me?fields=email');
            $this->FBUserEmail = array_key_exists('email', $emailArray) ? $emailArray['email'] : 'Onbekend';
            $this->loggedIn = true;
        }
        return $this->FBUserProfile;
    }
    public function getUserFullName() {
        return $this->FBUserProfile['name'];
    }
    public function getUserAlias() {
        return $this->FBUserProfile['username'];
    }
    public function getUserEmail() {
        return $this->FBUserEmail;
    }
    public function getUserFirstName() {
        return $this->FBUserProfile['first_name'];
    }
    public function getUserLastName() {
        return $this->FBUserProfile['last_name'];
    }
    public function getUserId() {
        return $this->FBUserProfile['id'];
    }
}

在我的控制器中,我存储或检索我的FBClass实例,如下所示:

public function checkFacebookState() {
    if (!$this->session->userdata('fb')) {
        $fbSes = new FBClass();
        $this->session->set_userdata('fb', serialize($fbSes));
    } else {
        $fbSes = unserialize($this->session->userdata('fb'));
    }
}

在会话中存储字符串确实有效。我知道PHP不是OO,这就是为什么我在寻找一个简单的替代方案。

有什么想法吗?

为了在会话中保存实例,无论是否首先序列化都没有问题。以下面的例子为例。在应用程序/库文件夹中创建库测试:

class Test {
    public $testProp;
    public function __construct() {
        $this->testProp = 'testing'; 
    }
}

然后在控制器中写入以下内容:

class Welcome extends CI_Controller {

    public function index()
    {
            $this->load->library('test');
            $this->session->set_userdata('someIns',  $this->test);
        $this->load->view('welcome_message');
    }
}

最后在您看来:

<div id="container">
    <?php 
        echo '<pre>';
        var_dump($x = $this->session->userdata('someIns'));
        echo '</pre>';
        echo '<pre>';
        echo $x->testProp;
        echo '</pre>';
        echo '<pre>';
        var_dump($this->session->all_userdata());
        echo '</pre>';
        ?>
</div>

输出将是:

object(Test)#15 (1) {
  ["testProp"]=>
  string(7) "testing"
}
testing
array(6) {
  //some session data
  ["user_data"]=>
  string(0) ""
  ["someIns"]=>
  object(Test)#15 (1) {
    ["testProp"]=>
    string(7) "testing"
  }
}

如果您注意到我在从会话中获取echo $x->testProp之后直接使用了它。此处不需要序列化。

带序列化:

控制器:

class Welcome extends CI_Controller {   
    public function index()
    {
            $this->load->library('test');
            $this->session->set_userdata('someIns', serialize($this->test));
        $this->load->view('welcome_message');
    }
}

视图:

    <div id="container">
        <?php 
            echo '<pre>';
            var_dump($x = $this->session->userdata('someIns'));
            echo '</pre>';
            echo '<pre>';
            $y = unserialize($x);
            echo $y->testProp;
            echo '</pre>';
            echo '<pre>';
            var_dump($this->session->all_userdata());
            echo '</pre>';
            ?>
    </div>

结果:

string(44) "O:4:"Test":1:{s:8:"testProp";s:7:"testing";}"
testing
array(6) {
  //some session data
  ["user_data"]=>
  string(0) ""
  ["someIns"]=>
  string(44) "O:4:"Test":1:{s:8:"testProp";s:7:"testing";}"
}

字CCD_ 2仍在被回显。

现在,你的问题显然是,你以错误的方式获得FB课程,这是一个图书馆。因此,您需要首先加载您的库,就像我在上面的示例中所做的那样

$this->load->library('test');
$this->session->set_userdata('someIns',  $this->test);

这里的$this->test是库文件夹中Test类的一个实例。