我跟着指示保存在容器令牌回調函數(https://github.com/tuupola/slim-jwt-auth):無法訪問令牌存儲在容器
$app = new \Slim\App();
$container = $app->getContainer();
$container["jwt"] = function ($container) {
return new StdClass;
};
$app->add(new \Slim\Middleware\JwtAuthentication([
"path" => ["/"],
"passthrough" => ["/version", "/auth"],
"secret" => "mysecret",
"callback" => function ($request, $response, $arguments) use ($container) {
$container["jwt"] = $arguments["decoded"];
},
"error" => function ($request, $response, $arguments) {
$data["status"] = "error";
$data["message"] = $arguments["message"];
return $response
->withHeader("Content-Type", "application/json")
->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
]));
沒有數據響應返回,似乎$這 - >智威湯遜是空的。
$app->get("/user", 'getUsers');
function getUsers($req, $res, $args) {
$decode = $this->jwt;
print_r($decode);
}
的可能的複製[訪問$這slim3路線內不工作「使用$這個時候不是在對象上下文」(http://stackoverflow.com/questions/40362978/access-this-在slim3-doesnt-work-using-this-when-not-in-objec) – jmattheis