Pragmatism in the real world

Introducing AkrabatSession

One of the requirements for a new app that I’m writing is that it has a specific session name. In Zend Framework 2, this is done by creating a SessionManager with the correct configuration and then setting the default manager on the Session Container:

use ZendSessionConfigSessionConfig;
use ZendSessionSessionManager;
use ZendSessionContainer;

$sessionConfig = new SessionConfig();
$sessionManager = new SessionManager($config);

Obviously, I need to be able to configure the name (and potentially other session configuration options) from my config/autoload/global.php file and this is a generically useful requirement, so I created the AkrabatSession module.

This is a really simple module that simply allows you to configure the SessionManager with minimal effort:

  1. Install AkrabatSession.
  2. Enable it as the first module in application.config.php
  3. Add the following to your configuration array in global.php:
        'session' => array(
            'name' => 'MY_SESSION_NAME_HERE',

Further details are in the README file and of course, it’s available on Packagist.

7 thoughts on “Introducing AkrabatSession

  1. hi there,

    i'd like to know if that's possible to share session between two subdomains (i meen cookies)

    for example by specifying a key like this

    public function getConfig()
    return array(
    'session' => array(
    'use_cookies' => true,
    'use_only_cookies' => true,
    'cookie_httponly' => true,
    'name' => 'ZF2_SESSION',
    'domain' => '' //<-

    thanks !!!

  2. Hello Rob! Thanks for all the great posts and helpful information!

    So I am just today learning ZF2, ran into this very issue. I need some configuration and session data available in my app. Easy in ZF1 using namespace in Zend_Session. But for some reason, I am just totally missing something simple in ZF2 with these modules. Makes sense how you configure, autoload, etc. But how the heck do I access the info contained, or even a method in my module? Everywhere I read online tell us how to create these things, but never do I see, "And here's how you access this in your view controller: $session = new AkrabatSession();" (just an example)

    I know there's something obvious I am missing here , so please forgive my ignorance.

    In your module (thanks for this BTW!) I have used composer to load into my /vendor folder. I loaded it in application.config.php, and I set teh global.php as you decribed. I run my view, no errors. But, soon as I try to "use AkrabatSession;" and add anything in my indexAction to get any of the data, i get "Class 'AkrabatSession' not found". I am using this the $session example above right now, but also tried to use AkrabatSession::getConfig() or similar.

    Thanks for your help! Looking forward to building with ZF2!

  3. Hi,

    AkrabatSession isn't for storing and retrieving session data, it's for configuring the session itself.

    To store and retrieve data session data, you use the ZendSession component. For example, in a controller you could do something like:

    $session = new ZendSessionContainer('SomeKeyName');
    // Set session:
    $session->pageNumber = 2;
    // Get from session:
    $pageNumber = $session->pageNumber;
  4. @Rob,
    Thank you! As Cor le Roux said, makes much more sense now, on the sessions part at least. The concepts with events, service manager and module use are going to take me some time to wrap my brain around, but like ZF1, in time I'll figure it out. My biggest problem is I've never been anywhere close to an expert coder, and I am still learning OOP concepts, so I have a big hill to climb. Learning this will be fun though! Thanks again for clearing that up for me!

  5. Hi,
    Thank you for the details. And it would be really helpful if you can explain how a session variable can be accessed in template i.e in layout (in fact all the templates ex: logged in username) …. Thank you very much

Comments are closed.