Hi
I was just reading about the release of CakePHP 3.0.0-dev3, and saw a mention that the session object will be going to live in side the Request object.
An unintended feature I currently enjoy in CakePHP 2.X is the ability to share the CakePHP session with legacy PHP code. This has allowed me to hook CakePHP into older projects, to allow new functionality to be written with CakePHP, without having to convert all the other code.
The way I do this is to make a small mode to the webroot/index.php, to prevent the dispatcher being called, if included from an external app, and then I manually start the session. All this gets done as a part of a program that is typically included by all of the legacy functionality. For me, that's the login check.
Here's how I do it:
logincheck.php
<?php
define('EXTERNAL_APP', true);
include_once $_SERVER['DOCUMENT_ROOT'].'/synapse/app/webroot/index.php';
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// suppress error messages for legacy code
App::uses('Configure', 'Core');
//check to see if server name is set (thanks Frank)
if (isset($_SERVER['SERVER_NAME'])) {
$serverName = $_SERVER['SERVER_NAME'];
} else if (isset($_SERVER['HOSTNAME'])) {
$serverName = $_SERVER['HOSTNAME'];
}
switch($serverName){
case '<live-host>':
Configure::write('debug',0);
break;
case '<staging-host>':
Configure::write('debug',0);
break;
default:
Configure::write('debug',0);
Configure::write('dojoSrc',1);
break;
}
App::uses('CakeSession', 'Model/Datasource');
$Session = new CakeSession();
$return = $Session->start();
if (!$return) {
error_log('The session was not started:'.$Session->error());
}
$found = $Session->read('Auth.User');
if(is_null($found)){
//log_message('debug', "Legacy Redirect to Login due to no session");
die('<script type="text/javascript">
window.location = "/synapse/users/login"
</script>');
}
Anyway, the thought occurred to me if the session should be moved inside the Request object, then this kind of bootstrapping might make any sort of upgrade path to CakePHP 3.0 so much harder that it would otherwise need to be (all legacy functionality would need to be converted to CakePHP 3.x, instead of just the CakePHP 2.x code we have already).
One of the things that started me on CakePHP was the ability for it to be integrated with legacy code. The only thing that really needed to be be shared was the session. Once the alpha is released with the changes to the session, I'll have a tool around and see if legacy integration is still possible.
I guess I'm drawing this method to your attention in a bid to try and have this functionality maintained. The number of people doing this sort of integration is probably very small. I guess if the session doesn't become dependent on the request, then I should be okay.
Regards
Reuben Helms