0

我正在研究 ZF2,并且我已经开发了自己的身份验证存储,但我想知道如何添加新的持久变量(类似会话)。

查看我的身份验证存储:

  <?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) {
        [...]
    }
}

我想在我的存储中添加一个名为foo的变量(我的会话?)

我试试这个,但它不起作用:

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

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

有任何想法吗 ?

4

3 回答 3

4

好的,我发现了一些东西,它对我有用:

我已经在我的身份验证存储类中添加了这些东西

use Zend\Session\Container;

然后,

    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;
    }

现在我可以在我的控制器中做这样的事情:

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

$container->foo = true;

if ($container->foo) {
// Congrats !
}
于 2013-06-05T09:27:28.963 回答
1

如何写最后登录时间的一个很好的例子。

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();
    }

}
于 2014-04-05T17:43:50.630 回答
0

检查本教程:

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

听起来您的个人 AuthStorage 应该Storage\Session像这样扩展:

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();
    }
}
于 2013-06-05T08:53:07.317 回答