0

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) ?

Chilion
  • 4,380
  • 4
  • 33
  • 48
  • I've not used Laravel, however searching the term `flashdata` it sound just like the `FlashMessenger` plugin in ZF2. So are you looking to just add additional data to the session (such as an `array`, rather than a message string)? – AlexP Sep 09 '14 at 11:22
  • Yes. Im looking to post an array with additional data to the next request (par example; which jQuery tab should be openend) – Chilion Sep 09 '14 at 11:28

2 Answers2

1

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">    
AlexP
  • 9,906
  • 1
  • 24
  • 43
  • Great answer, but, it only answers my second question. How are the thought of using the session for data-exchange? It's is possible to do it with the url (eg. url/tab1/) but I prefer clean url's without mentioning the tab I'm on. Thats where I thought of the session. – Chilion Sep 09 '14 at 12:40
  • Sessions are fine, although I don't think that I would be doing so server side (as you would need a new request), if you just want to remember the current tab. Perhaps you could should consider [a javascript soultion](http://stackoverflow.com/questions/10523433/how-do-i-keep-the-current-tab-active-with-twitter-bootstrap-after-a-page-reload)? – AlexP Sep 09 '14 at 12:51
  • Its a post and then a redirect if form has success. I want to return to the tab where the initial form was editted. The tabs are jQuery already. – Chilion Sep 09 '14 at 13:50
  • @Chilion I've updated my answer based on your comments. – AlexP Sep 09 '14 at 15:03
  • great. It was more like an example, none the less, usefull! I indeed also use Ajax, so I'm sending a request. Thanks for your help and explanation. – Chilion Sep 10 '14 at 07:25
0

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.

Tom
  • 522
  • 3
  • 13