Git Product home page Git Product logo

expressive-zend-session's Introduction

Zend Session middleware for Zend Expressive

Provides Zend Session integration for Expressive.

Install this library using composer:

$ composer require xorock/expressive-zend-session

Usage

I suggest installing Expressive Configuration Manager

Zend Session has built-in ConfigProvider class, responsible for automatic registration of the components. With Expressive Configuration Manager You can register all factories with just single line:

$configManager = new ConfigManager([
    \Zend\Session\ConfigProvider::class,
    new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
], 'data/config-cache.php');

Now, Zend Session will search Your merged config file for predefined keys. Please refer to Zend Session documentation.

As an example, We can create following file:

session.global.php

use Zend\Session\Storage\SessionArrayStorage;

return [
    'session_config' => [
        'name' => 'SID',
        'cookie_httponly' => true,
        'cookie_path' => '/',
        'cookie_secure' => false,
        'use_cookies' => true,
        'cookie_lifetime' => 3600,
        'save_path' => '/temp/data/session',
    ],
    'session_storage' => [
        'type' => SessionArrayStorage::class
    ],
];

Note: There is a bug in SessionManager which can lead to fatal error when using SessionStorage class.

Now You can register middleware:

middleware-pipeline.global.php

use Mylab\Session\ZendSessionMiddleware;
use Mylab\Session\ZendSessionMiddlewareFactory;

// ...
'dependencies' => [
    'factories' => [
        Helper\ServerUrlMiddleware::class => Helper\ServerUrlMiddlewareFactory::class,
        Helper\UrlHelperMiddleware::class => Helper\UrlHelperMiddlewareFactory::class,
        ZendSessionMiddleware::class => ZendSessionMiddlewareFactory::class,
    ],
],
'middleware_pipeline' => [
    'always' => [
        'middleware' => [
            Helper\ServerUrlMiddleware::class,
            ZendSessionMiddleware::class,
        ],
        'priority' => 10000,
    ]
]

Middleware injects SessionManager to Container so You can get it with:

use Zend\Session\Container;

Container::getDefaultManager();

How can I use Session Save Handler?

When created, SessionManagerFactory searches Container for addition keys. One of them is

$saveHandler = $container->get(SaveHandlerInterface::class);

Simply, if you wish to attach a save handler to the manager, you will need to write Your own factory, and assign it to the service name "Zend\Session\SaveHandler\SaveHandlerInterface", (or alias that name to your own service).

expressive-zend-session's People

Contributors

xorock avatar

Watchers

James Cloos avatar Vassilis Kanellopoulos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.