Session 如何在Zend Framework 2中的自定义AuthStorage中添加特定变量

Session 如何在Zend Framework 2中的自定义AuthStorage中添加特定变量,session,authentication,interface,zend-framework2,storage,Session,Authentication,Interface,Zend Framework2,Storage,我正在研究ZF2,并且我已经开发了自己的用于身份验证的存储,但是我想知道如何添加一个新的持久变量(类似会话) 查看我的身份验证存储: <?php namespace Application\Model; use Zend\Authentication\Storage; use Zend\Authentication\Storage\StorageInterface; use Zend\ServiceManager\ServiceManagerAwareInterface; use

我正在研究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) {
        [...]
    }
}
有什么想法吗?

查看本教程:

听起来您的个人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();
    }
}
检查本教程:

听起来您的个人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();
    }
}

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

我已经在auth存储类中添加了这些东西

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

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

我已经在auth存储类中添加了这些东西

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

如何编写上次登录时间的一个很好的示例

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

}

如何编写上次登录时间的一个很好的示例

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

}

不,我的存储空间和你的一样。因此它不会从
Storage\Session
Hmmm扩展。请尝试以下操作:对于setter,而不是
$this->foo=value
尝试
$this->getStorage()->foo(value)
。我认为你不能仅仅用$this->foo来访问它,你必须使用存储对象。它不起作用。这不是一个持久变量。PS:查找,我找到了解决方案否,我的存储与之相同。因此它不会从
Storage\Session
Hmmm扩展。请尝试以下操作:对于setter,而不是
$this->foo=value
尝试
$this->getStorage()->foo(value)
。我认为你不能仅仅用$this->foo来访问它,你必须使用存储对象。它不起作用。这不是一个持久变量。PS:查找,我找到了解决方案