PHPackages                             session-interop/middleware.async - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. session-interop/middleware.async

ActiveLibrary[HTTP &amp; Networking](/categories/http)

session-interop/middleware.async
================================

This package contains a zend's middleware to be able to inject a Session Object into the request

053.6k2[1 issues](https://github.com/session-interop/middleware.async/issues)PHP

Since Jul 21Pushed 8y ago3 watchersCompare

[ Source](https://github.com/session-interop/middleware.async)[ Packagist](https://packagist.org/packages/session-interop/middleware.async)[ RSS](/packages/session-interop-middlewareasync/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

SessionMiddleware
=================

[](#sessionmiddleware)

This middleware inject a [`SessionInterface`](https://github.com/session-interop/session-interop) as an [attribute of a psr7's request's](http://www.php-fig.org/psr/psr-7/#3-2-1-psr-http-message-serverrequestinterface). The attribute's name is defined as Introp\\Session\\SessionInterface::class. It **does not** reconfigure the session if $\_SESSION exists. This middleware work in two steps:

1. This middleware open the session (by calling `session_start`) if needed to ***copy*** it to create an instance extending the [`ArraySession`](https://github.com/session-interop/utils.session) then close it immediatly (by calling `session_abort`) if it was not started before, to preserve the session state. Once the session copied, it is injected in the PSR7's request and then followings middleware are called.
2. Once every following middleware has been executed, news / removed / modified session values (on the object) is wrote in $\_SESSION and then the session is persisted. Once again, it ensure to reopen the session or to let it close depending on the previous session's state.

**Warning:** Persisted mean there is a manual call to `session_write_close`, that imply all $\_SESSION to be wrote.

Recommanded
-----------

[](#recommanded)

We recommands to place this middleware as soon as possible in the pipe of your application, this way every following middleware will be able to use the request's session.

Required
========

[](#required)

This middleware is designed to be used as a [zend-stratigility](https://github.com/zendframework/zend-stratigility) middleware.

Usage
-----

[](#usage)

If you want to change the configuration, you must provide a [`SessionConfigurationInterface`](https://github.com/session-interop/session-configuration-interop) implementation as the factory parameter. Current server's configuration is used by using the package [`DefaultSessionConfiguration`](https://github.com/session-interop/utils.defaultmanager/blob/master/src/SessionConfiguration.php)

Following example are assuming you use

- [`zend-expressive`](https://github.com/zendframework/zend-expressive) and a container compatible with
- A [`container-interop`](https://github.com/container-interop/container-interop) as dependencies container

\###Basic Usage:

```
  // Without container

  $mySessionConfiguration = null;
  // If you have custom configuration:
  $mySessionConfiguration = new MySessionConfiguration();

  $sessionMiddleware = \Interop\Session\Middleware\Async\AsyncSessionMiddlewareFactory::createFromConfiguration($mySessionConfiguration);
  // $mySessionConfiguration is optional
  $app->pipe($sessionMiddleware);
  // OR using container
  $factory = new \Interop\Session\Middleware\Async\AsyncSessionMiddlewareFactory();
  $container->set(Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider::class, $factory->__invoke($container));
  //....
  $app->pipe(
    $container->get(\Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider::class)
  );
```

Now in every following Middleware:

```
$session = $request->getAttribute(\Interop\Session\SessionInterface::class);
$session->set("foo", "baz");
//...
echo $session->get("foo"); // print baz
```

### Using service providers:

[](#using-service-providers)

[`service-provider`](https://github.com/container-interop/service-provider). This following example use [`Simplex`](https://github.com/mnapoli/simplex)If the provider find an instance of [`SessionConfigurationInterface`](https://github.com/session-interop/session-configuration-interop) it will be used. To be possible the container **must** inject the instance inside the container using the name `Interop\Session\Configuration\SessionConfigurationInterface`. This name is got by using:

```
use Interop\Session\Configuration\SessionConfigurationInterface;
//.....
SessionConfigurationInterface::class
```

```
$container->register(
  new \Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider()
);
/// .............
$app->pipe(
  $container->get(\Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider::class)
)
```

Now in every following Middleware:

```
$session = $request->getAttribute(\Interop\Session\SessionInterface::class);
$session->set("foo", "baz");
echo $session->get("foo"); // print baz
```

### Using Aura.DI

[](#using-auradi)

```
$factory = "\\Interop\\Session\\Middleware\\Async\\AsyncSessionMiddlewareFactory";
$name = "\\Interop\\Session\\Middleware\\Async\\AsyncSessionMiddleware";
$container->set($factory, $container->lazyNew($factory));
$container->set($name, $container->lazyGetCall($factory, '__invoke', $container));

/// .......

$app->pipe($container->get("\\Interop\\Session\\Middleware\\Async\\AsyncSessionMiddleware"));
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/b1953426bdefad774b92f15505637dcf22e19f567acd596131d86ce40f738f2a?d=identicon)[Ngob](/maintainers/Ngob)

---

Top Contributors

[![Ngob](https://avatars.githubusercontent.com/u/2749238?v=4)](https://github.com/Ngob "Ngob (14 commits)")[![xhuberty](https://avatars.githubusercontent.com/u/8350192?v=4)](https://github.com/xhuberty "xhuberty (1 commits)")

### Embed Badge

![Health badge](/badges/session-interop-middlewareasync/health.svg)

```
[![Health](https://phpackages.com/badges/session-interop-middlewareasync/health.svg)](https://phpackages.com/packages/session-interop-middlewareasync)
```

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78026.4M414](/packages/react-http)[php-http/curl-client

PSR-18 and HTTPlug Async client with cURL

48347.0M384](/packages/php-http-curl-client)[smi2/phpclickhouse

PHP ClickHouse Client

84310.1M71](/packages/smi2-phpclickhouse)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
