Menu

Session
Using sessions with Leaf

Overview

Leaf v1.3.0 comes along with support for the native PHP session. We have directly tied it into leaf's core as a module, so you can use it without middleware as done in some other API based frameworks.

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().

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..both as strings

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

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