0

I've installed the following composer packages:

"require": {
 "beatswitch/lock": "0.2.0",
 "league/event": "2.1.2",
 "oscarotero/psr7-middlewares": "v3.16.1",
 "pdepend/pdepend": "2.2.4",
 "sebastian/phpcpd": "2.0.4",
 "instaclick/php-code-sniffer": "1.4.2",
 "phploc/phploc": "3.0.1",
 "phpmd/phpmd": "2.4.3",
 "phpunit/phpunit": "5.5.x-dev",
 "phpunit/phpunit-selenium": "3.0.2",
 "league/route": "3.x-dev",
 "guzzlehttp/psr7": "1.3.0",
 "relay/relay": "2.x-dev",
 "monolog/monolog": "2.0.x-dev",
 "aura/session": "3.x-dev"

}

and now I want to "glue" these packages together with the psr7-middlewares packages. Unfortunately I couldn't find any info on how to use each library from these middlewares outside of the dispatcher. Basic things like Access Logging and Routing work already without problems.

I integrated Aura Sessions like the following:

Middleware::AuraSession(),
    ->factory(new Aura\Session\SessionFactory) //(optional) Intance of Aura\Session\SessionFactory
    ->name('my-session-name'), //(optional) custom session name

function ($request, $response, $next) {
    //Get the session instance
    $session = AuraSession::getSession($request);

    return $response;
}

This works definetly with Relayphp and Guzzle\Http message interface. Dumping the Session Variable gives me the basic Session data with empty values, because i didnt set anything.

Unfortunately I couldn't find any easy to understand info on how I can set the Session inside the middleware-system. At the End I got an Response and can get its body and Headers but not more.

How can I access the function set in the middlewares array? I tried also to just use the AuraSession Class and set Variables over that class but it doesnt work. Or do I have to set all Sessions before dispatching the middlewares together? But how are they then integrated into it?

hardking
  • 193
  • 2
  • 17

1 Answers1

1

I have overlooked that I can set Sessions inside of the anonymous function which I have to implement in my middleware list like stated before (but didnt realize it though), so lets say I want to create a Session when a user logs in, with Aura Session and PSR7-Middlewares it could look like the following:

function ($request, $response, $next) {
    //Get the session instance
    $session = AuraSession::getSession($request);

    $auth = new server\modules\authentication;

    $auth->setUserSession($request, $session);

    return $response;
}

And the method setUserSession could be:

public function setUserSession ($request, $session) {
    $segment = $session->getSegment(get_called_class());
    if ($this->loggedIn && $segment->get('logged_in', false)) {
        $segment->set('logged_in', true);
    }
}
hardking
  • 193
  • 2
  • 17