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

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

问题描述:

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
]

我正在使用Zend Framework 3和SessionManager,我试图建立一个控制器插件/视图帮助器来显示确认对话后 验证一些数据。 这个想法只是设置一个Session变量,其中包含确认对话需要的所有内容,通过视图读取它并取消设置。 但即使这个简单的循环也失败了 插件在控制器调用时基本上执行此操作: p>

  $ dataArray = [
'seat_data'=> 布局中的'data'
]; 
 $ this-> sessionManager-> getStorage() - > confirmDialog = $ dataArray; 
  code>  pre> 
 
 

。 phtml我调用我的视图Helper执行此操作: p>

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

我确实将sessionManager注入插件和视图助手。 当在接收到数据后没有清除变量时,我从会话变量中获取更改的数据,并且它应该由插件更新。但是当在第一次读取变量后清除变量时,它始终为空。 p> \ n

这是我的global.php设置: p>

 'session_manager'=>  [
'验证者'=>  [
 RemoteAddr :: class,
 HttpUserAgent :: class,
] 
],
'session_storage'=>  [
'type'=>  SessionArrayStorage :: class 
] 
  code>  pre> 
  div>

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.