How to use Zend\Session in zf2?

扶醉桌前 提交于 2019-11-27 11:33:34

问题


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?

Also I can not find any examples in the internet.


回答1:


Some examples of zf2 sessions usage:

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'); 



回答2:


Definitely yes, you should use Zend\Session\Container

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

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. Manager is a facade for Storage and SaveHandler and it's configured with ConfigInterface in order to save your session data in DB or Memcache server.




回答3:


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

Zend\Authentication\Storage\Session.php

Maybe you can find your answer there.




回答4:


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.

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




回答5:


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




回答6:


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.



回答7:


To start a session you need to use

zend\session\container


来源:https://stackoverflow.com/questions/8990195/how-to-use-zend-session-in-zf2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!