XF 2.1 Using session data?

asprin

Active member
From my controller, I'm trying to save a session key but that's throwing an error:
Instances of XF\Db\AbstractAdapter cannot be serialized or unserialized in src\XF\Db\AbstractAdapter.php on line 680

My code:
PHP:
$session = $this->session();
$session->set('foo', 'bar');

If I comment out the $session->set() statement, then the error is gone. What am I missing here?
 
The code here looks fine to me, but I'm thinking you need to save the session after you set that. Assuming you're not already doing that, you should be able to use $session->save();

I'm not sure if that will fix your issue but, without more info, I'm afraid this is as much help as I can offer.
 
Tried session save as well. But same error.

I'm not sure if that will fix your issue but, without more info, I'm afraid this is as much help as I can offer.
Practically that's all the info I've. My public controller contains the aforementioned lines and then a call to the view function. To elaborate, this is what the controller code looks like:

PHP:
public function actionFoo()
{
    $session = $this->session();
    $session->set('foo', 'bar');
    $session->save();
    
    return $this->view('Foo\Bar:doFoo', 'my_template_name', []);
}
 
Tried session save as well. But same error.


Practically that's all the info I've. My public controller contains the aforementioned lines and then a call to the view function. To elaborate, this is what the controller code looks like:

PHP:
public function actionFoo()
{
    $session = $this->session();
    $session->set('foo', 'bar');
    $session->save();
   
    return $this->view('Foo\Bar:doFoo', 'my_template_name', []);
}

I'm about to install an instance of XF 2.1 on a test server then I'll try to re-create this error to see if I can help you figure it out.
 
Tried session save as well. But same error.


Practically that's all the info I've. My public controller contains the aforementioned lines and then a call to the view function. To elaborate, this is what the controller code looks like:

PHP:
public function actionFoo()
{
    $session = $this->session();
    $session->set('foo', 'bar');
    $session->save();
  
    return $this->view('Foo\Bar:doFoo', 'my_template_name', []);
}

I just installed XF 2.1.12 on my test server. I created the following controller:

PHP:
<?php

namespace Test\Pub\Controller;

use XF\Pub\Controller\AbstractController;
use XF\Mvc\ParameterBag;

class Test extends AbstractController
{
    public function actionIndex(ParameterBag $params)
    {
        $session = $this->session();
        $session->set('foo', 'bar');
        $session->save();
       
        return $this->view('Function\Test', 'template_test', []);
    }
}

Then the following template:

HTML:
<xf:title>Test page</xf:title>

<div class="block">
    <div class="block-container">
        <div class="block-body block-row">
            {$xf.session.foo}
        </div>
    </div>
</div>

Using this, everything is working just as expected, a page that simply contains the word "bar". Evidently you've got something else going on. Have you tried disabling all add-ons to see if it still gives the same error?
 
Back
Top Bottom