To store any data within a session in ZF2 you can (or rather should) use an instance of Zend\Session\Container
.
Each 'container' accepts a 'namespace' parameter that allows you to maintain session information independently between containers.
For instance the Zend\Mvc\Controller\Plugin\FlashMessenger
that you have mentioned internally uses a session container with a specific namespace, FlashMessenger
.
You can create and add any data to a session container (Check out the Session Storage
for more info, the default is Zend\Session\Storage\ArrayStorage
)
use Zend\Session\Container;
$container = new Container('my_custom_namespace');
$container->foo = array('bar');
$container->hello = 'test';
Edit (Tabs)
The issue you will have with tabs is when you click on a new tab you will not be sending a new HTTP request (unless you use AJAX). Therefore you will need to either store the current tab in local storage or a cookie from javascript.
You can also pass the current tab via GET or POST parameters. I personally append to the HTML anchor #
between requests (rather than storing it within sessions).
A basic example might be
// Append the currently selected tab in the anchor
$(document).on('shown.bs.tab', 'ul.nav-tabs > li > a', function (e) {
window.location.hash = $(e.target).attr("href").substr(1);
});
// check if there is already a value and display that tab
var hash = window.location.hash;
if (hash) $('.nav-tabs a[href="' + hash + '"]').tab('show');
So any URL with a anchor will show the matching tab. If you are posting from a form you can add it to the action attribute.
<form id="my-form" method="post" action="/the/form/target#tab-3">