Skip to content
Permalink
Browse files

Include a session on the PSR7 request.

Having access to the session on the request will be handy in middleware
objects as it can be useful to have authentication data available.

Refs #6960
  • Loading branch information...
markstory committed Jun 24, 2016
1 parent 38c903b commit 77c8c7e75c140a2e01e95fa24bba748b0a72bc18
@@ -15,6 +15,7 @@
namespace Cake\Http;
use Cake\Core\Configure;
use Cake\Network\Session;
use Cake\Utility\Hash;
use Zend\Diactoros\ServerRequestFactory as BaseFactory;
@@ -39,8 +40,16 @@ public static function fromGlobals(
) {
$request = parent::fromGlobals($server, $query, $body, $cookies, $files);
list($base, $webroot) = static::getBase($request);
$sessionConfig = (array)Configure::read('Session') + [
'defaults' => 'php',
'cookiePath' => $webroot
];
$session = Session::create($sessionConfig);
$request = $request->withAttribute('base', $base)
->withAttribute('webroot', $webroot);
->withAttribute('webroot', $webroot)
->withAttribute('session', $session);
if ($base) {
$request = static::updatePath($base, $request);
}
@@ -259,7 +259,7 @@ public function testToCakeBaseSessionPath()
$psr = ServerRequestFactory::fromGlobals($server);
$cake = RequestTransformer::toCake($psr);
$this->assertEquals('/thisapp', ini_get('session.cookie_path'));
$this->assertEquals('/thisapp/', ini_get('session.cookie_path'));
}
/**
@@ -45,6 +45,25 @@ public function tearDown()
$_SERVER = $this->server;
}
/**
* Test fromGlobals includes the session
*
* @return void
*/
public function testFromGlobalsUrlSession()
{
Configure::write('App.base', '/basedir');
$server = [
'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
'PHP_SELF' => '/index.php',
'REQUEST_URI' => '/posts/add',
];
$res = ServerRequestFactory::fromGlobals($server);
$session = $res->getAttribute('session');
$this->assertInstanceOf('Cake\Network\Session', $session);
$this->assertEquals('/basedir/', ini_get('session.cookie_path'), 'Needs trailing / for cookie to work');
}
/**
* Test fromGlobals with App.base defined.
*

0 comments on commit 77c8c7e

Please sign in to comment.
You can’t perform that action at this time.