PHP code example of andrewdyer / slim3-session-middleware

1. Go to this page and download the library: Download andrewdyer/slim3-session-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andrewdyer / slim3-session-middleware example snippets




$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();

$container = $app->getContainer();

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

$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']);
});