Documentation/Container/Services/Session

Sessions

The user session mechanism in the HLEB2 framework is provided by the Session service — a simple wrapper around PHP's session management functions.

Examples of using Session in controllers (and all classes inheriting from Hleb\Base\Container), such as retrieving a value from a session:

// variant 1
use Hleb\Reference\SessionInterface;
$value $this->container->get(SessionInterface::class)->get('session_name');

// variant 2
$value $this->container->session()->get('session_name');

Example of accessing a session in application code:

// variant 1
use Hleb\Static\Container;
use 
Hleb\Reference\SessionInterface;
$value Container::get(SessionInterface::class)->get('session_name');

// variant 2
use Hleb\Static\Session;
$value Session::get('session_name');

The Session object can also be obtained through dependency injection via the Hleb\Reference\Interface\Session interface.

To simplify examples, the following will only include access through Hleb\Static\Session.

In the standard Session service implementation, methods appropriately use the global $_SESSION variable.


#get()

The get() method retrieves session data by parameter name.

use Hleb\Static\Session;

$value Session::get('session_name');

#set()

The set() method allows assigning session data by name.

use Hleb\Static\Session;

Session::set('session_name''value');

#delete()

The delete() method removes session data by name.


#clear()

The clear() method removes all session data.


#all()

The all() method returns an array with all session data.


#getSessionId()

The getSessionId() method returns the current session identifier.
The session identifier can be modified in the 'session.name' configuration setting in the /config/system.php file, and is initially set to 'PHPSESSID'.


#Asynchronous Mode

In asynchronous use of the framework, the methods of the Session service function similarly, but a different mechanism for setting and reading them is used.

DB Cookies

Page translated: chatgpt 4-o
Back to top