Archive for the ‘CakePHP’ category

Changing the Session ID in CakePHP

October 20th, 2009

Ever wanted to set your own session id?

Well, I did. I was creating a webservice where authentication would be done with a ‘token’.

This token would simply be a random guid given to that user after he logged on with his username/password.
Now, this random guid/string would be the session ID.
That way, if the user does an action with that token, we can easily know who it is.

But enough with the ‘why I’ wanted to do it. You’re here for yourself right?, so you’ll be interested in some sample code.

In basic php you’d do it by giving the ID parameter to the session_id() method

//taken from php.net
string session_id  ([ string $id  ] )

But how can we do this in CakePHP?
One possibility would be by implementing sessions ourself. But then why use a framework that all those nice components?

The session component has the ability to set the session id. This must be done in the beforefilter method of your controller.
Doing it later would be to late, as the session would already be started.

So in your controller you’d have something like this:

public $components = array('Session');
    public function beforeFilter() {
        $sessionid = 'myownsessionid';
 
        if($sessionid != null && $sessionid != '') {
            $this->Session->id($sessionid);
        }
    }

There you go :) , it’s that easy!

CakePHP: Extending the AppController

October 19th, 2009

Ever wanted to split off functionality to a base class other then AppController?

This is what was needed to prevent typing the same code over and over again in a project of mine.

The app controller is great and all, but it’s just one class and you might want some functionality in only a part of your controllers.

So lets get to work, what do we have?

  • AppController(app/app_controller.php)
  • ExtendedAppController(app/extended_app_controller.php)
  • MyController(app/my_controller.php)

Extending the app controller isn’t much work, all you need to know really is to put “App::import(‘Controller’, ‘ExtendedApp);” above the MyController.

Lets put it in code:

//app controller
class AppController extends Controller {
}
 
//extended app controller
class ExtendedAppController extends AppController {
 
}
 
//MyController
App::import('Controller', 'ExtendedApp);
class MyController extends PipeMsgHandlerController {
 
}

You don’t necessarily need to extend from AppController. If you have functionality in your app controller that you do not want to share with the ExtenedAppController then another option is to extend Controller.

Although I don’t recommend it, it’s better to split off the functionality you don’t want in the ExtendedController in another controller that inherits from the app controller for those controllers.

XML to Array and Array to Xml in CakePHP

October 19th, 2009

When working with xml allot, you’ll find that this little ‘trick’ might come in handy.

Editing large xml structures is a pain. So why not convert them to a nice array?

It’s done like this:
The xml:

<PipeMsg>
    <Header>
        <Task>Logon</Task>
	<AuthenticationToken>sdfdg4-hkjty45-4544-sdfdsf4</AuthenticationToken>
	<CreationDstamp>54641215</CreationDstamp>
    </Header>
    <Body>
	<UserName>Crazy</UserName>
	<Password>cinderella </Password>
    </Body>
</PipeMsg>

Now convert to an array:

$objXml = 'the xml in the blok above';
$arrXml = Set::reverse($objXml;);
 
echo $arrXml['Header']['Task'];
//result
Logon

We can also go the other way around, in CakePHP we do it like this:

//$arrXml = array from example above
App::Import('Helper', 'Xml');
$objXmlHelper = new XmlHelper();
$objXml = $objXmlHelper->serilize($arrXml);