Menu

Session
Using sessions with Leaf

Overview

Leaf v1.4.2 has thrown in a few more stuff since our session support in v1.3. We have a few more methods and better usability to offer. Let's check it out.

Including Session

To include the Session object in a route, use this:

use Leaf\Core\Http\Session; $session = new Session(); $leaf->post('/session/start', function() use($session, $request) { $session->set('name', 'Michael Darko'); });
Here, we set $session to the Session class, and we pass it into the route with use().

In v1.4.2, starting a new session will automatically generate a session id for you.

Session Methods

Starting a new session

Starting a new session with leaf is actually very simple😅..

$session = new Leaf\Core\Http\Session;
This line of code checks for an active session....if none is found, it creates a new session, if one is running, it just instanciates the Session object to be used in Leaf

set()

set() simply adds another key value pair to the native $_SESSION array. It takes in 2 parameters: The array key and it's pair.

use Leaf\Core\Http\Session; $session = new Session(); $leaf->post('/name/add', function() use($session) { $name = $session->set('name', 'Michael Darko'); });

remove() new in v1.4.2

remove() simply deletes a session variable. It takes in 1 parameter: the key of the variable to delete.

use Leaf\Core\Http\Session; $session = new Session(); $leaf->post('/name/remove', function() use($session) { $name = $session->remove('name'); });

reset() new in v1.4.2

reset() simply re-initialises a session.

use Leaf\Core\Http\Session; $session = new Session(); $leaf->post('/session/reset', function() use($session) { $session->reset(); });

id() new in v1.4.2

id() sets and/or returns the current session id. It takes in an optional parameter: the ID to overwrite the session id.

use Leaf\Core\Http\Session; $session = new Session(); $leaf->post('/user/add', function() use($session) { $id = $session->id(); $newId = $session->id("new session id"); });

regenerate() new in v1.4.2

regenerate() simply generates a new session id. It takes in 1 parameter, whether to delete all session data or not(boolean)

use Leaf\Core\Http\Session; $session = new Session(); $leaf->post('/session/regenerate', function() use($session) { $session->regenerate(false); $session->regenerate(true); // will clear all session data });

destroy()

destroy() ends a session and deletes all it's data

use Leaf\Core\Http\Session; $session = new Session(); $leaf->post('/session/destroy', function() use($session) { $session->destroy(); });

getParam()

getParam is a method built for APIs, but can be used in a normal web app/website. It takes in one parameter: the name of the param passed into the app through the session It works just like how $_SESSION['key'] does

use Leaf\Core\Http\Session; $session = new Session(); $leaf->post('/name/add', function() use($session) { $name = $session->getParam('name'); });

getBody()

getBody() returns the key => value pairs of all the session data including any CSRF data

use Leaf\Core\Http\Session; $session = new Session(); $leaf->post('/name/add', function() use($session) { $body = $session->getBody(); });

Next Steps

Simple Routing
Response
Simple Authentication
Database Config