In services.php you create a session service, which you can use to create and read sessions throughout your application. Setting session variables can be done in controllers and if you want to access the variable in the main theme you could use a Controller base to extend from. Below is how i solved it in my authentication i hope this helps
// start a session service in services.php
$di->setShared('session', function () {
$session = new SessionAdapter();
$session->start();
return $session;
});
// store information in a session, for example if a user logged in
$this->session->set('auth-identity', [
'id' => $user->id,
'username' => $user->name,
'image' => $user->image
]);
// Set the session variables in a view via a controller
$this->view->setVar('uid', $this->session->get('auth-identity')['id']);
// or if you want to access it in the main layout create a ControllerBase
use Phalcon\Mvc\Controller;
class ControllerBase extends Controller
{
public function onConstruct()
{
$this->view->setVar('uid', $this->session->get('auth-identity')['id']);
$this->view->setVar('userName', $this->session->get('auth-identity')['username']);
$this->view->setVar('userImage', $this->session->get('auth-identity')['image']);
}
}
// And extend the other controllers where you want to access the session variables from it like
class YourController extends ControllerBase
{
// your controller actions
}