andrewdyer/slim3-session-middleware

Session middleware and helper for Slim 3.

0.1.0 2018-08-15 11:32 UTC

This package is auto-updated.

Last update: 2024-11-17 22:57:21 UTC


README

Codacy Badge Latest Stable Version Latest Unstable Version License Total Downloads Daily Downloads Monthly Downloads composer.lock available

Simple session middleware for the Slim Framework. Also included is a useful helper class that allows PHP built-in session management.

License

Licensed under MIT. Totally free for private or commercial projects.

Installation

composer require andrewdyer/slim3-session-middleware

Usage

<?php

$app = new \Slim\App();

$app->add(new \Anddye\Middleware\SessionMiddleware([
    'autorefresh'   => true,
    'name'          => 'myapp_session',
    'lifetime'      => '1 hour',
]));

$app->get('/', function (Request $request, Response $response) use ($container) {
    if (!isset($container['session']['loggedIn'])) {
        //...
    }
    ///..
});

$app->run();

Supported Options

Session Helper

The \Anddye\Session\Helper class can be attached to your app container:

$container = $app->getContainer();

$container['session'] = function ($container) {
    return new \Anddye\Session\Helper();
};

The helper class can be used to check if a session variable exists in addition to setting, getting and deleting session variables.

$app->get('/', function (Request $request, Response $response) use ($container) {
    // Check if variable exists
    $exists = $container['session']->exists('my_key');
    $exists = isset($container['session']->my_key);
    $exists = isset($container['session']['my_key']);

    // Get variable value
    $value = $container['session']->get('my_key', 'default');
    $value = $container['session']->my_key;
    $value = $container['session']['my_key'];

    // Set variable value
    $container['session']->set('my_key', 'my_value');
    $container['session']->my_key = 'my_value';
    $container['session']['my_key'] = 'my_value';

    // Delete variable
    $container['session']->delete('my_key');
    unset($container['session']->my_key);
    unset($container['session']['my_key']);
});

Support

If you are having general issues with this library, then please feel free to contact me on Twitter.

If you believe you have found an issue, please report it using the issue tracker, or better yet, fork the repository and submit a pull request.

If you're using this package, I'd love to hear your thoughts!

Useful Links