dongyinglan8707 2017-10-05 07:37
浏览 47
已采纳

取消设置和重置后,会话变量为空

Im using Zend Framework 3 and the SessionManager and im trying to build a controller plugin / view helper to display confirm dialogues after validating some Data . The idea was simply to set a Session variable with everything the confirm dialogue needs, reading it by the view, and unsetting it. But even this simple cycle fails. The plugin basically does this when invoked by the controller:

$dataArray = [
        'some_data' => 'data'
    ];
    $this->sessionManager->getStorage()->confirmDialog = $dataArray;

in the layout.phtml i call my view Helper which does this:

public function __invoke() {
    $data = $this->sessionManager->getStorage()->confirmDialog;
    $this->sessionManager->getStorage()->clear('confirmDialog');
    return $this->getDialog($data);
}

I do inject the sessionManager to both the plugin and the view helper. When not clearing the variable after receiving its data i get the changed data from the session variable and it gets updated by the Plugin as it should.But when clearing the variable after the first time reading it, its always empty.

Here my global.php setup:

'session_manager' => [
    'validators' => [
        RemoteAddr::class,
        HttpUserAgent::class,
    ]
],
'session_storage' => [
    'type' => SessionArrayStorage::class
]
  • 写回答

1条回答 默认 最新

  • dqwmhrxt68679 2017-10-05 09:08
    关注

    Because the value is passed by reference, when you clear it, you clear the read information with it also.

    As I mentioned in a comment, I suggest using the default falsh messenger plugin, but if you want to create your own plugin, here is a hint from that's source code, which shows you how you can achieve a default clear after read from session.

    https://github.com/zendframework/zend-mvc-plugin-flashmessenger/blob/843654a029a19c38e0c3b2e940e59edec75c3e4f/src/FlashMessenger.php#L165

    This setting is actually tells the session container to drop that information after '1 hop', ie. in case of a next request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境