How to use zf3-way to get sessonid? I operate session like "$sessionContainer = new Container('api');"


In my zf3 model, I use the following way to operate my session stuff.
$sessionContainer = new Container(‘api’);

Now I need to get sessionid in my logic, if use the pure php, it will like:
$sessionId = session_id();

I wonder if there is a zf3-official way to get sessionid.



Do you create a zend-mvc based application?


yes! :slightly_smiling_face: :slightly_smiling_face:


Here is the standard way of using zend-session within a zend-mvc-based application:

  1. install zend-session per Composer

  2. enable zend-session as module


return [
    'Zend\Session', // <-- Add this line
  1. extend the conifguration


return [
    // ...
    'session_config'  => [],
    'session_storage' => [
        'type' => Zend\Session\Storage\SessionArrayStorage::class,
  1. add the session-manager to the controller


namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Session\SessionManager;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
     * @var SessionManager
    private $sessionManager;

    public function __construct(SessionManager $sessionManager)
        $this->sessionManager = $sessionManager;

    // ...
  1. update the controller configuration


return [
    'controllers'  => [
        'factories' => [
            Controller\IndexController::class => \Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory::class,
    // ...
  1. use the session-manager


    public function indexAction()

        return new ViewModel();

(The example is based on the zend-skeletion-application.)

The configuration is documented at:
The session-manager is described at:

And you can get the session-id also with PHP’s standard function session_id. Nothing else is done in the session-manager:


Hi Froschdesign, thank you so much!

$this->sessionManager->getId(); It is the right zf3 way solution!

I will use your way to improve my image drag auth plugin as I asked with topic how to get $container in static method. I saw you kindly answer me as well. thank you again.