PHP code example of davidecesarano / embryo-session

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

    

davidecesarano / embryo-session example snippets


class TestSetSessionMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $session = $request->getAttribute('session');
        $session->set('name', 'World');
        $response = $handler->handle($request);
        return $response->write('Hello '.$session->get('name').'</p><p><a href="test.php">Other Page</a></p>');
    }
}

class TestGetSessionMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $session = $request->getAttribute('session');
        $response = $handler->handle($request);
        return $response->write('Hello '.$session->get('name').'</p>');
    }
}

$session = new Session;
$middleware = new RequestHandler;
$middleware->add(
    (new SessionMiddleware)
        ->setSession($session)
        ->setOptions([
            'use_cookies'      => false,
            'use_only_cookies' => true
        ])
);
$middleware->add(TestSetSessionMiddleware::class);
$middleware->add(TestGetSessionMiddleware::class);
$response = $middleware->dispatch($request, $response);

$session->get('key', 'default');

$session->all();

if ($session->has('key')) {
    //...
}

$session->set('name', 'value');

$session->flash('name', 'value');

$session->remove('name');
$session->clear();