Giter VIP home page Giter VIP logo

embryo-session's Introduction

Embryo Session

Middleware to start a php session using the request data and close it after return the response.

Requirements

Installation

Using Composer:

$ composer require davidecesarano/embryo-session

Usage

Creates a middleware for setting session item:

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

Creates another middleware for getting session item:

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

Adds middleware to dispatcher:

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

Options

setName(string $name)

The session name. If it's not provided, use the php's default.

seOptions(array $options = [])

Array of options passed to session_start().

setSessionRequestAttribute(string $name)

The session request attribute. If it's not provided, use $request->getAttribute('session').

Collection

Retrieving data

You may retrieve an item from the session and you may also pass a default value as the second argument to the get method:

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

Retrieving all session data

If you would like to retrieve all the data in the session, you may use the all method:

$session->all();

Determining if an item exists in the session

To determine if an item is present in the session, you may use the has method. The has method returns true if the item is present and is not null:

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

Storing data

The set method may be used to set a new value onto a session:

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

Flash data

You may wish to store items in the session only for the next request using the flash method:

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

Deleting data

The remove method will remove a piece of data from the session. If you would like the remove all data from the session, you may use the clear method:

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

embryo-session's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.