4webby BLOG

01 11 2007

SMAKE: SMF forum - CakePHP integration

by Daniel | hits(54283)

TAGS: cakephp smf integration forum

Today I'll present you a convinient way of integrating the Simple Machine Forum (SMF) into your CakePHP applications.

Well the problem is that the SMF DB table used to store sessions does not follow the CakePHP conventions, or said the other way around, CakePHP doen't understand SMF session table structure.

SMF stores its sessions in a table called forumprefix_sessions. Let's assume in the rest of our article that the forumprefix is smf_ (you can set the forum prefix table while installing SMF). This table is missing a couple of fields that CakPHP needs to write its sessions:

  • the 'id' field (primaryKey)
  • the 'data' field

Right!

CakePHP DB sessions are handled by the Session library (ROOT/cake/libs/Session.php). We don't want to hack the Session library directly, but simply find a convenient way to extend it, so that if we update our CakePHP installation, we won't overwrite the core Session library. Furthermore going the way I'm presenting here, our code will be more tidy, and we won't get crazy on every upgrade.

For this tutorial I'm using:

Let's start then!

1) Put the SMF forum in app/webroot/forums/

2) Set CAKE_SESSION_COOKIE to be the default 'PHPSESSID'.

3) Modify the session handling functions in Cake to use the MySQL table used by SMF (forumprefix_sessions)

4) in my app/bootstrap.php I added this line at the end

require_Once('forum/SSI.php');

NOW WE NEED TO TELL CAKE TO USE SMF session table (here I'll assume that your SMF DB tables prefix is *smf_*

THE SMF sessin table doesn't have an id column, but rather a *session_id* one

5) rename cake/libs/session.php in *session_cake.php*

6) rename the class in your new *session_cake.php* into

class CakeOriginalSession extends Object {  

7) create a file in cake/libs/ called *session_smf.php* (this extends the original Cake Session lib that we moved in *session_cake.php*).
go in the bin and copy and past what you will find in here:
http://bin.cakephp.org/view/1933866502

into the newly created file.

8) create a file in cake/libs/ called *session.php* with the following in it.

require_once('session_smf.php'); class CakeSession extends SmfSession { } 

9) hack the original smf_sessions table as follows:

  • add a column called 'expires' and set it to NULL (only CakePHP writes there)
  • set the `last_update` field to NULL (only SMF writes there)
CREATE TABLE `smf_sessions` (
`session_id` char(32) NOT NULL default '',
`last_update` int(10) unsigned default NULL,
`data` text,
`expires` int(11) default NULL,
PRIMARY KEY (`session_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

10) somewhere in you template write the following

  <?php echo ssi_welcome()?>

11) how can you get the SMF session data then?

try this:

$user = (unserialize($this->Session->read('THE_LOGIN_COOKIE_NAME_YOU_SET_IN_SMF_CONFIG')));
echo $user[0];

This will give you the id of the currently logged in user in SMF.

What's missing yet?
An helper or component that behaves as a wrapper for the functions in SSI.php

Dan

PS: a different approach is presented here: CakePHP & SMF

view/hide comments | add comment

  • Luke

    2009-01-08 12:56:29

    Hi

    can you not in Cake specify the session class to use session_id as the id variable, as you can in general in Cake models, for eaxmple?

    Also, have you tried this with later versions of Cake? I guess it should still work as you take a extending approach to the code and wisely try to avoid the core classes. I wonder if there is another place the /libs/ session class you create could be put in the app/ code ?

  • Daniel

    2009-01-08 13:26:11

    Hey Luke,

    well with regard to your first question, I don't think so, but I might be wrong ;o)

    Second question:
    I didn't try it with the latest CakePHP 1.2. stable: if you try it please let me know if it works!

    Third question:
    you could try to put it in /vendors or /app/venodors and import it with App:import('Vendor', 'Session')

    Hope this helps!

    Dan

  • Luke

    2009-01-12 12:08:34

    Hi Dan

    thanks for your nice reply :)

    that's all good!

    Have you ever managed to make single unified log-in (sign in) for Smake then / SFM and Cake? Is it possible? e.g. a user signs in to the cake app, and this also signs in to SMF.

    I have not used SMF before so am just wodnering how easy it is to do this.

  • Thomas

    2009-10-01 03:01:04

    I've made all the steps explained but it didn't work because i was using one database for smf and another for cakephp (newest version of cakephp 1.2 stable btw).
    Here's what to do:
    1) after the installation of smf in the app/webroot/forums, you make new database configurations with cake bake shell, one (i named it default) for your cakephp database and one (i named it smf) for your smf database
    2) you put the database configuration name for smf (for me it's smf) in your config/core.php Session.database
    3) all steps explained by the howto match, but instead of using the above code for session_smf.php you use this one (it's the original one above reedited by me):
    http://bin.cakephp.org/view/341239884
    I hope i've explained everything, if someone has questions or something doesn't work as it should, please ask...
    Great howto btw... :)

hide comments | add comment

4webby.com

Tags

powered by 4webby.com