PHP code example of dwendrich / expressive-session-middleware

1. Go to this page and download the library: Download dwendrich/expressive-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/ */

    

dwendrich / expressive-session-middleware example snippets


$aggregator = new ConfigAggregator([
 
    // enable SessionMiddleware
    SessionMiddleware\ConfigProvider::class,
    
    // ... other stuff goes here 
 
    // Load application config in a pre-defined order in such a way that local settings
    // overwrite global settings. (Loaded as first to last):
    //   - `global.php`
    //   - `*.global.php`
    //   - `local.php`
    //   - `*.local.php`
    new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
 
    // Load development config if it exists
    new PhpFileProvider('config/development.config.php'),
], $cacheConfig['config_cache_path']);

// Register session handling middleware
$app->pipe(SessionMiddleware::class);
 
// Register the routing middleware in the middleware pipeline
$app->pipe(\Zend\Expressive\Router\Middleware\RouteMiddleware::class);
$app->pipe(ImplicitHeadMiddleware::class);
$app->pipe(ImplicitOptionsMiddleware::class);
$app->pipe(UrlHelperMiddleware::class);

$app->route(
    '/path-to-my-action',
    [
        SessionMiddleware::class,
        MyApp\Action\MyAction::class
    ],
    ['GET'],
    'path-to-my-action'
);

/**
 * Process an incoming server request and return a response, optionally delegating
 * to the next middleware component to create the response.
 *
 * @param ServerRequestInterface $request
 * @param DelegateInterface $delegate
 *
 * @return ResponseInterface
 */
public function process(ServerRequestInterface $request, DelegateInterface $delegate) : ResponseInterface
{
    $sessionManager = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE, false);
    
    if ($sessionManager) {
        // sessionManager is present and can be used
    }
    
    // further request processing goes here...
}

use Zend\Session\Container;
 
$container = Container('my_namespace');
 
// save 'foo' into the `item` key
$container->item = 'foo';

use Zend\Session\Container;
 
$container = Container('my_namespace');
 
// read the content from the `item` key
$foo = $container->item;

return [
    'session' => [
        'config' => [
            'options' => [
                'name' => 'my_special_session_name',
                'use_cookies' => true,
                'cookie_secure' => false,
            ],
        ],
    ],
];

return [
    'session' => [
        'class' => Zend\Session\Config\StandardConfig::class,
        'options' => [
            'name' => 'my_app',
        ],
    ],
];

return [
    'session' => [
        'storage' => new MyApp\Session\StorageAdapter::class,
    ],
];

return [
    'session' => [
        'validators' => [
            Session\Validator\RemoteAddr::class,
            Session\Validator\HttpUserAgent::class,
        ],
    ],
];