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:
$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😅..
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.
remove()
remove() simply deletes a session variable. It takes in 1 parameter: the key of the variable to delete.
reset()
reset() simply re-initialises a session.
id()
id() sets and/or returns the current session id. It takes in an optional parameter: the ID to overwrite the session id.
regenerate()
regenerate() simply generates a new session id. It takes in 1 parameter, whether to delete all session data or not(boolean)
destroy()
destroy() ends a session and deletes all it's data
get()
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
getBody()
getBody() returns the key => value pairs of all the session data including any CSRF data
Next Steps
Simple RoutingResponse
Simple Authentication
Database Config