Putting Session in Request for CakePHP 3.0

171 views
Skip to first unread message

Reuben Helms

unread,
May 6, 2014, 11:15:19 PM5/6/14
to cakeph...@googlegroups.com
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

Christian Winther

unread,
May 7, 2014, 11:44:25 AM5/7/14
to cakeph...@googlegroups.com
Dispatcher::dispatch takes an CakeRequest instance, so if you use composer or auto-load, you can create the request object your self and dispatch the request with the right session engine - I think

mark_story

unread,
May 7, 2014, 12:36:59 PM5/7/14
to cakeph...@googlegroups.com
I don't think putting sessions as part of the request object will break this at all. There will still be a separate session object that is put *inside* the request. You should be able to use that request object in external code. I would guess that what you're doing will be easier when there are fewer global statics in CakePHP.

-Mark

Reuben Helms

unread,
May 7, 2014, 5:18:55 PM5/7/14
to cakeph...@googlegroups.com
Super sweet to hear
--
You received this message because you are subscribed to a topic in the Google Groups "cakephp-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cakephp-core/nxuKtLr6oDI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cakephp-core...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages