Variables stored in session middleware not persisting across different routes


#1

Hi guys,

I am currently building a web application in Javascript that queries for information from a PHP REST API (which I’ve built using Slim). Additionally, I am using session middleware (akrabat’s) that allows me to store “global” variables inside the index.php file (an APIManager class instance) inside the session. Upon landing on the main page of the application, I send an ajax request to my PHP REST API telling it to instantiate the session, as well as the APIManager.

However, when I query another route from the frontend Javascript (a route that searches for information using the api manager), the APIManager shows up as null(). Looking at the slim server console, I know that the “/” route was hit, and thus, the session and APIManager should both be initialized already.

My question is: Why is the APIManager null inside my other route?

Here is the code in question:
$app->get('/', function (Request $req, Response $res) {
$session = new \RKA\Session();
$api = new APIManager();
$session->set('api', $api);
});

$app->get('/search', function (Request $req, Response $res) {
$session = new \RKA\Session();
$api = $session->get('api'); // why is this null?
});

Thanks a lot guys!


#2

Maybe there is an issue in serializing/deserializing the object. Can you try persisting a scalar such as a string to see if that works? e.g. $session->set('api', 'test')