My problem is that I can’t access a global variable in my callback function.
I basically try to pass the user id into the callback function. The user id is stored in a global variable called $user (I’m using UserSpice for handling user login). It is accessible outside the callback.
Here is a small code snippet that demonstrate the problem:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require("../core/init.php"); // loads $user
require 'vendor/autoload.php';
$user_id = $user->data()->id; // user is available here
$app = new \Slim\App;
$app->get('/app_get_user_info', function (Request $request, Response $response) {
$user_id = $user->data()->id; // This throws an error. Why? How can I access $user here?
});
$app->run();
?>
P.S.
I tried the following solution which was proposed online somewhere:
$app = new \Slim\App;
$app->foo = 'bar’
but I keep getting $app->foo = null inside the callback.