Zend Flashdata存在吗?

In Laravel, CodeIgniter etc we do have the function to set some data in the Session for 1 request.

Does something like this exist for Zend? Should one even want to use this or is this considered a bad example?

Of course I know about the flashMessenger, but this is only intended for messages as the name says already. I googled about it alot, but cannot seem to find anything related to this topic.

So, to be clear, I want to know the following:

  • Is it a bad example or habit to use the session for data that is just used one request?

  • Does Zend Framework 2 include something like a (Laravel example) Session::flashdata($key, $value) ?

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">    

See the namespace expiration section of the Zend Session "Advanced Usage" documentation. You can use a call to Zend_Session_Namespace::setExpirationHops(1) to emulate the 'flash' methods in Laravel.


EDIT: Sorry, I didn't realise that it had been removed in ZF2. Apparently, it does exist in ZF2, but they have moved it to the new container system, which seems to have replaced the old namespace system.