Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working on ZF2 and I have developped my own storage for authentication but I wonder how to add a new persistent variable (session-like).

Look My Auth Storage :

  <?php

namespace Application\Model;

use Zend\Authentication\Storage;
use Zend\Authentication\Storage\StorageInterface;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use Application\Model\User;

class MyAuthStorage implements Storage\StorageInterface, ServiceManagerAwareInterface
{

    protected $storage;
    protected $userTable;
    protected $resolvedIdentity;
    protected $serviceManager;


    public function isEmpty() {
        [...]
    }

    public function read() {
        [...]
    }

    public function write($contents) {
        [...]
    }

    public function clear() {
        [...]
    }

    public function getStorage() {
        [...]
    }

    public function setStorage(Storage\StorageInterface $storage) {
        [...]
    }

    public function getUserTable() {
        [...]
    }

    public function getServiceManager() {
        [...]
    }

    public function setServiceManager(ServiceManager $serviceManager) {
        [...]
    }
}

I would like to add a variable called foo in my storage (my session ?)

I try this, but it doesn't work :

protected $foo;
        public function setFoo($value) {
            $this->foo= $value;
        }

        public function getFoo() {
            return $this->foo;
        }

Any ideas ?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Ok, I found something and it works for me :

I've added these things in my auth storage class

use Zend\Session\Container;

Then,

    protected $container;

    public function setContainer(Container $container) {
        $this->container = $container;
        return $this->container;
    }

    public function getContainer() {
        if (!isset($this->container)) {
            $this->setContainer(new Container('myauthstorage'));
        }
        return $this->container;
    }

And now I can do in my controller stuff like that :

$container = $this->getServiceLocator()->get('AuthService')->getStorage()->getContainer();  

$container->foo = true;

if ($container->foo) {
// Congrats !
}
share|improve this answer

A good example how to write last login time.

namespace Application\Model;

use Zend\Authentication\Storage;

class AuthStorage extends Storage\Session
{

    public function setRememberMe($rememberMe = 0, $time = 1209600)
    {
        if ($rememberMe == 1) {
            $this->session->getManager()->rememberMe($time);
        }
    }

    public function forgetMe()
    {
        $this->session->getManager()->forgetMe();
    }

    public function lastLogin()
    {
        $this->session->{$this->getMember()}->last_login = time();
    }

}
share|improve this answer

Check this tutorial :

http://samsonasik.wordpress.com/2012/10/23/zend-framework-2-create-login-authentication-using-authenticationservice-with-rememberme/

Sounds like your personnal AuthStorage should extends Storage\Session like this :

namespace SanAuth\Model;

use Zend\Authentication\Storage;

class MyAuthStorage extends Storage\Session
{
    public function setRememberMe($rememberMe = 0, $time = 1209600)
    {
         if ($rememberMe == 1) {
             $this->session->getManager()->rememberMe($time);
         }
     }

    public function forgetMe()
    {
        $this->session->getManager()->forgetMe();
    }
}
share|improve this answer
    
No, my Storage is the same of github.com/ZF-Commons/ZfcUser/blob/master/src/ZfcUser/…. So it doesn't extend from Storage\Session –  duques_l Jun 5 '13 at 9:03
    
Hmmm. Try this : for the setter, instead of $this->foo = value try $this->getStorage()->foo(value). I think you can't just access it with $this->foo, you have to use the storage object. –  Tounu Jun 5 '13 at 9:23
    
It doesn't work. It's not a persistent var. PS: Look up, I found the solution –  duques_l Jun 5 '13 at 9:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.