Learn DooPHP: High performance PHP framework

Quick guide to DooSession

This will be a quick guide how to use DooSession, first of all you need to call a DooSession class, I love to have one super class that extends DooController class. In super class constructor I make session and create everything I need for a website.

Calling session class needs one parameter and thats namespace name, often you wish to put name for your session.

$this->_application = Doo::session("mywebsite");

Now that you created session namespace you can start using it, storing in session and getting stuff from it. For example you want to define username of user that is registered:

$this->_application->user = "John";

Now you are accessing it from controller with:

$this->_application->user

Another nice thing is to have access to session from your view scripts, I am doing it like this, inside constructor of my super class I add:

$data['application'] = Doo::session("mywebsite");
$this->renderc('templatefile', $data);

Now from your view scripts you can access your user variable like:

$this->data['application']->user

Thats about it, all functions that you have in doo session class are in API.

http://doophp.com/documentation/api_svn


  • Is the $this->_application exists in the DooController or is that a custom class property?

  • Nope that is some variable defined in superclass that extends DooController, I gave example, I assigned DooSession object to $this->_application :)

    I have something like this

    class CoreController extends DooController {
    
    	/**
    	* Instace of DooSession
    	*/
    
    	public $_application = null;
    
    	/**
    	* Base path of application
    	*/
    
    	public $_basePath = null;
    
    	/**
    	* Instace of DooSession
    	*/
    	public $_application = null;
    
    	/**
    	* Instance of DooAcl
    	*/
    
    	public $_acl = null;
            public function __construct() {
    		$this->_basePath = Doo::conf()->ROOT_DIR;
    		// add some globals that we need
    		$this->_db = Doo::db();
    		$this->_view = DooController::view();
    		$this->host = Doo::conf()->host;
    		$this->_view->host = Doo::conf()->host;
    		// ACL
    		$this->_acl = Doo::acl();
    		// add sessions
    		$this->_application = Doo::session("mysite");
    	}
    }
    

    So as you can see all my controllers extend CoreController, and in them I use $this->_application or in view script $this->application but you can name it how you like.
    Sorry if this article was little confusing I wrote it in the hurry and my english is not that perfect.

  • OK, now I understood, thanks :)

  • First of all, why would u call a session storage variable for _application ? it dosent say anything about what that property contains.

    Second.. the _ prefix in your variable declerations are commonly used for private variables so that is confusing also.

  • it’s a personal preference? but I think $appdata might be nicer :P

  • Well thats just example from my application, Henrik thats true but naming stuff for your own application is matter of preference :)

    Well good thing are that you told me what is confusing so I can fix that, I was writing it in a hurry.

  • Sure i can understand what it does (shouldnt take a genius) but you are sharing code so why not use the best practices, and in 3 month time when u revisit your application in some child controller theres a high chance you wont remember what $_application means.

  • You got point there, dont worry about me, problem is its confusing for others, next time I will make it less confusing :)

  • Hi Milos,
    I only, from your permission, wish to add a working variant of an example of use DooSession.

    1. In protected/config/common.conf.php add the next line:

    set_include_path($config['SITE_PATH'].’protected/class’);

    2. In protected/config/routes.conf.php add line:

    $route['*']['/learn'] = array(’DemosessionController’, ‘index’);

    3. In the folder protected/class create file CoreController.php

    _basePath = Doo::conf()->BASE_PATH;
    # add some globals that we need
    $this->_db = Doo::db();
    $this->_view = DooController::view();
    $this->_host = Doo::conf()->SITE_PATH;
    # ACL
    $this->_acl = Doo::acl();
    # add sessions
    $this->_application = Doo::session(”{$this->sesname}”);

    # set some data to current session
    $this->_application->user_name = “imnobel”;
    $this->_application->nick_name = “Nobel”;
    }
    }

    ?>

    4. In the folder protected/controller create file DemosessionController.php

    data['user'] = $this->_application->get(”user_name”);
    $this->data['nick'] = $this->_application->get(”nick_name”);

    $this->view()->render(’demosession/index’, $this->data);
    }
    }

    ?>

    5. In the folder protected/view/demosession create file index.html

    Simple Session Testing

    Simple Session Testing
    My username is {{user}}
    And my nickname is {{nick}}

    OK.
    Now go to the our new page (example: http://doo.cc/learn/) to look in action our demo application.

  • Hi, tnx for making example, anyway it will be in new demo I am making for DooSessio, DooForm and DooMailer. It will be done when I am done with DooMailer class.

  • Hi milos,

    I try Doo::session but it gives me error call to undefined methode. I check in the Doo.php file and dont see any session() function. I can simply add it like logger function, but it’s not good to modify Doo system/core file. Did i miss something so that i could not use the methode from Doo singleton ?

    Currently my work around is loading the DooSession class manually.

    Doo::loadCore(’session’.DIRECTORY_SEPARATOR.DooSession’);
    $session = new DooSession(’mysite’);
    $session->name = ‘myname’;

    And i loose netbeans intellisense for $session :( .

  • Ah, now my netbeans suddenly show the intellisense for $session.

  • Check if you downloaded latest trunk from SVN, you should have inside dooframework/Doo.php

    [code]
    /**
    * @return DooSession
    */
    public static function session($namespace = null){
    if(self::$_session===NULL){
    self::loadCore('session/DooSession');
    self::$_session = new DooSession($namespace);
    }
    return self::$_session;
    }
    [/code]

    What version are you using?

  • I’m sure i use Trunk Revision 389 but It’s missing the session function in the Doo.php file. It’s wierd but i rechecking again now, it works with Trunk Revision 389.
    Thanks.

  • Hi Milos,

    I’ve try to use doo session for several time and I’m very confused bacause it’s still keep error.

    Can you please tell me complete guide or step by step to make doo session really work?

    Thank you

  • I have a problem.
    I have written class CoreController extends DooController and I have added the line to the common.conf.php : set_include_path($config['SITE_PATH'].’protected/class’);
    When I have tried use my CoreController :
    class MainController extends CoreController {

    I see ERROR :
    Class ‘CoreController’ not found. What did I do wrong.

  • I have not looked at the DooSession in detail. I assume this would only work for unclustered servers? And that a different session would need to be implemented to store session data in, say, a database?

  • @Gribo

    Dont do that use Doo::loadController(’CoreController’);

    @wrlee

    I would store sessions in APC or memcache if you have multiple servers and one central not db.

  • Hrrrr, “session()” doesn’t works!
    The key is a version. In the latest official version 1.2 no “session()” method. But API Doc at site is about trunk version.
    Don’t break your brains! ;) Luck!

  • APC – “Alternative PHP Cache” … even more I need to learn! :-)

    I just posted to the forum… have there been any thoughts about changing DooSession to be an abstract class or interface with which the current class can derive or implement. This would allow alternate implementations to be created that hold to the same semantics.

    I guess I going to have to move to the “unofficial” SVN release since this is not in the current release, 1.2.

  • Yeah I was thinking about that however you can always use Doo::loadClass(’YourSessionClass’);

    And with it you can handle your sessions, make static functions for getting and setting in apc and you can use it in your whole application:

    MySessionClass::get(’something_you_want_from_session’);
    MySessionClass::set(’something’);

    Not many users will use their own session classes and when you are working with high load sites you dont need to use apache sessions, you can create your own session class that will store session information in memory and that will be much faster.

  • For a “framework,” it is the value to creating an Interface or abstract class is to enforce the semantics of the interface–even if it is simple. This allows a clear, consistent way in which any kind of “session” class can be implemented. The practical advantage is that an application can chose, depending on its installation environment, the kind of session that fits best.

    I think it is worth “codifying” this as an interface rather than a stand-alone class. The discussion about whether the other public methods should be part of that interface can also be discussed.

  • I took a quick look at the DooSession code… what is the distinction between the namespace and the session ID? In your example, you are setting the namespace and not the session ID.

    I am inferring that the settings in your example’s session will be site-wide for all users of the application. If I want to keep user specific settings, then I should create a separate session object that specifies a specific session ID (probably using the same namespace)?

  • Session namespaces are used to separate session data, they dont have anything to do with session id. In constructor you can pass $namespace argument which allows developers to partition session data into separate namespaces. It is just way to secure session state against changes due to accidental naming collisions.

  • I’d asked about the session ID because it is the 2nd optional parameter to the constructor.

  • (oops, I hit the submit button too early)

    … so I wanted to understand the distinction between the namespace and ID. If I have two separate “users” using the application, if I do not specify a session ID for either, what would be the state of the session that both users see? Would specifying unique session IDs make sessions states unique for each user?

  • That optional param is used to set session id:
    PHP Manual
    session_id() is used to get or set the session id for the current session.
    Session namespaces dont have anything to do with session id, session namespaces are just used to separate session data so you dont have naming collisions.

  • @wrlee look at the session id being like a variable $someArray for example and then instead of all the session data going in like $someArray = array(’name’=>’someValue’, ‘xss_check’ => 123);

    The namespace lets you have nested arrays in the session so its more like having an array defined as:
    $someArray = array(’namespace’ => array(’name’=>’someValue’, ‘xss_check’ => 123), ‘anothernamespace’ => array(’foo’=>’bar’, ‘xss_check’ =>’this_wont_collide with the other now’));

    Thats really whats going on in the background…a little simplified but gives you a coded way of seeing the use of the session namespace.

  • Thank you Richard maybe your approach is better for understanding :)