PHP code example of antidot-fw / session

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

    

antidot-fw / session example snippets




$sessionFactory = new AuraSessionFactory();
$sessionMiddleware = new SessionMiddleware($sessionFactory($container));



use Antidot\Application\Http\Application;
use Antidot\Application\Http\Middleware\RouteDispatcherMiddleware;
use Antidot\Session\Application\Http\Middleware\SessionMiddleware;
...

return static function (Application $app) : void {
    $app->pipe(...);
    $app->pipe(SessionMiddleware::class); // added here
    $app->pipe(RouteDispatcherMiddleware::class);
    ...
};



// src/Handler/SomeHandler.php

declare(strict_types=1);

namespace App\Handler;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\HtmlResponse;

class SomeHandler implements RequestHandlerInterface
{

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        /** @var \Antidot\Session\Application\Http\SessionSegment $session */
        $session = $request->getAttribute('session');

        $session->set('foo', 'bar');
        $session->set('baz', 'dib');
        $message = $session->get('foo'); // 'bar'
        $message = $session->get('baz'); // 'dib'
        $session->setFlash('message', 'Hello world!');
        $message = $session->getFlash('message'); // 'Hello world!'
        ...
    }
}



declare(strict_types=1);

namespace Antidot\Session\Application\Http;

interface SessionSegment
{
    public function get(string $identity);
    public function getFlash(string $identity);
    public function set(string $identity, $value): void;
    public function setFlash(string $identity, $value): void;
}



declare(strict_types=1);

namespace Antidot\Session\Application\Http;

use Psr\Http\Message\ServerRequestInterface;

interface SessionSegmentFactory
{
    public function __invoke(ServerRequestInterface $request): SessionSegment;
}