php 如何在 zf2 中使用 Zend\Session?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8990195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:56:08  来源:igfitidea点击:

How to use Zend\Session in zf2?

phpsessionzend-framework2

提问by rdo

Does anybody try zf2? I can not understand new mechanism of using sessions in zf2. How can I write and read to/from the session in new zend framework?

有人试过zf2吗?我无法理解在 zf2 中使用会话的新机制。如何在新的 Zend 框架中写入和读取会话?

Also I can not find any examples in the internet.

另外我在互联网上找不到任何例子。

回答by rdo

Some examples of zf2 sessions usage:

zf2 会话使用的一些示例:

Session creation:

会话创建:

use Zend\Session\Container;
$session = new Container('base');

Check that key exists in session:

检查会话中是否存在密钥:

$session->offsetExists('email')

Getting value from the session by key:

通过密钥从会话中获取价值:

$email = $session->offsetGet('email');

Setting value in session:

会话中的设置值:

$session->offsetSet('email', $email);

Unsetting value in session:

在会话中取消设置值:

$session->offsetUnset('email');

And other easy way to use session are:

使用会话的其他简单方法是:

$session = new Container('foo');

// these are all equivalent means to the same end

// 这些都是达到相同目的的等效手段

$session['bar'] = 'foobar';

$session->bar = 'foobar';

$session->offsetSet('bar', 'foobar'); 

回答by dVaffection

Definitely yes, you should use Zend\Session\Container

肯定是的,你应该使用Zend\Session\Container

Container extends of ArrayObjectand instantiates with ARRAY_AS_PROPSflag that means you can easily iterate through properties and read/write them, e.g.

容器扩展ArrayObject并使用ARRAY_AS_PROPS标志实例化,这意味着您可以轻松地遍历属性并读取/写入它们,例如

use Zend\Session\Container as SessionContainer;

$this->session = new SessionContainer('post_supply');
$this->session->ex = true;
var_dump($this->session->ex);

First argument is session namespace and second — Manager. Manageris a facade for Storageand SaveHandlerand it's configured with ConfigInterfacein order to save your session data in DB or Memcache server.

第一个参数是会话命名空间,第二个参数是ManagerManagerStorageand 的一个外观SaveHandler,它被配置ConfigInterface为将您的会话数据保存在 DB 或 Memcache 服务器中。

回答by ohartl

I'm currently working with zf2. I found usage of Sessions in:

我目前正在使用 zf2。我在以下方面发现了 Sessions 的用法:

Zend\Authentication\Storage\Session.php

Zend\Authentication\Storage\Session.php

Maybe you can find your answer there.

也许你可以在那里找到你的答案。

回答by musafar006

If you are trying to use session in your login action, you can use: "Zend\Authentication\AuthenticationService". It Authenticates the user and store session as well.

如果您尝试在登录操作中使用会话,则可以使用:“ Zend\Authentication\AuthenticationService”。它还对用户和存储会话进行身份验证。

getStorage()->write($contents)will store the session.

getStorage()->write($contents)将存储会话。

回答by zoomi

well here is the brief example. i have implemented regarding maintaining session on successful authentication of user.

这是一个简短的例子。我已经实施了关于在用户成功验证时维护会话的方法。

<?php
$registry = Zend_Registry::getInstance();
$DB = $registry['DB'];
$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
$authAdapter->setTableName('user');
$authAdapter->setIdentityColumn("user_name");
$authAdapter->setCredentialColumn("user_password");
//get values
$username = $request->getParam('username');
$password = $request->getParam('password');
//set values
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);

$auth = Zend_Auth::getInstance();
//to store in session
$auth->setStorage(new Zend_Auth_Storage_Session('front'));

$authResult = $auth->authenticate($authAdapter);
if ($authResult->isValid()) {
    $authAdap = $authAdapter->getResultRowObject(null, "Password");
    $auth->getStorage()->write($authAdap);
    $this->_redirect('/login/controlpannel');
} else {
    $this->_redirect('/login/login');
}
?>

getting values or check data stored in session related to user

获取值或检查存储在与用户相关的会话中的数据

<?php
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('front'));
 if($auth->hasIdentity()){
     $data = $auth->getStorage()->read();
     print_r($data);
 }else{
     $this->_redirect('/login/login');
 }
?>

hope this could help someone

希望这可以帮助某人

回答by Hajis Hakkim

use Zend\Session\Container; 

public function createAction(){
  $session = new Container('name');
  $session->offsetSet('session_variable', $value);
}
//the above codes are used for create session.

public function take_valuesAction(){ 
  $session = new Container('name');
  echo $value = $session->offsetGet('session_variable');
}
//the above codes are used for take values from session.

public function destroyAction(){ 
  $session = new Container('name');
  $session->getManager()->destroy();
}
//the above codes are used for destroy the session.

回答by michaelbn

To start a session you need to use

要开始会话,您需要使用

zend\session\container