ZF2中的会话 [英] Sessions in ZF2

查看:99
本文介绍了ZF2中的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能告诉我如何在ZF2中正确使用会话吗?到目前为止,我有以下代码:

Could you tell me how to properly use sessions in ZF2? So far I have this code:

"session" =>
[
    "remember_me_seconds" => 2419200, 
    "use_cookies" => true,
    "cookie_httponly" => true
]

这是我从stackoverflow上的一些帖子中复制的会话配置.现在,我应该将此代码放在每个使用会话的模块中还是在应用程序模块中的module.config.php中?

That's the session config I copied from some post here on stackoverflow. Now should I put this code into module.config.php in each module that uses sessions or in the Application module?

public function onBootstrap(EventInterface $Event)
{
    $Config = $Event->getApplication()->getServiceManager()->get('Configuration');
    $SessionConfig = new SessionConfig();
    $SessionConfig->setOptions($Config['session']);
    $SessionManager = new SessionManager($SessionConfig);
    $SessionManager->start();  
    Container::setDefaultManager($SessionManager);
}

Module类的onBootstrap()方法存在相同的问题.该代码应该进入每个模块的Module类,还是进入应用程序的Module类一次?

Same problem with the onBootstrap() method of the Module class. Should this code go into each module's Module class or just once into the Application's Module class?

在两种情况下,我都尝试了两种方法,甚至尝试将这段代码同时放入两个模块中,但是我唯一能做的就是在控制器的构造函数中设置会话变量,然后在操作/方法中读取它们.我无法在一种操作/方法中设置会话变量,然后在另一种方法/方法中读取它.如果删除在控制器的构造函数中设置变量的行,则在会话中将不再看到这些变量.该会话的行为就像每次请求页面时创建和删除的会话一样.

In both cases I have tried both approaches and I even tried putting this code into both modules at once, but the only thing I was able to accomplish was to set session variables in controller's constructor and then read them in actions/methods. I wasn't able to set a session variable in one action/method and then read it in another. If I remove the lines in which I set the variables in controller's constructor, I can no longer see these variables in the session. The session just behaves like it was created and deleted each time a page is requested.

我错过了什么吗?请不要将我链接到互联网上的任何资源,我已经阅读了所有内容,但它们并没有真正的帮助.

Am I missing something? Please don't link me to any resources on the internet, I have read them all and they're not really helpful.

推荐答案

在Zend Framework 2中,您无需进行任何配置即可使用会话.当然,您可以更改设置,但是如果您只想起床并运行会话,那么现在就不用担心了.

You don't need to do any configuration to use sessions in Zend Framework 2. Sure, you can change settings, but if you just want to get up and running with sessions, then don't worry about it for now.

抱歉,但我将无视您的最后一句话;大约一个月前,我写了一个关于此的文章主题,目的是展示如何快速开始使用ZF2中的会话.它在搜索引擎中排名不高,因此您可能没有读过它.

My apologies, but I am going to disregard your last sentence; about a month ago, I wrote an article about this subject with the purpose of showing how to quickly get started with using sessions in ZF2. It doesn't rank well in search engines, so chances are you haven't read it.

这是一个代码片段,显示了如何完成此操作.如果您对它在后台的工作方式感兴趣,请参考上面的链接.

Here is a code snippet showing how it can be done. If you are interested in how it works behind the scenes, then please refer to the link above.

namespace MyApplication\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Session\Container; // We need this when using sessions

class UserController extends AbstractActionController {
    public function loginAction() {
        // Store username in session
        $userSession = new Container('user');
        $userSession->username = 'Andy0708';

        return $this->redirect()->toRoute('welcome');
    }

    public function welcomeAction() {
        // Retrieve username from session
        $userSession = new Container('user');
        $username = $userSession->username; // $username now contains 'Andy0708'
    }
}

这篇关于ZF2中的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆