【发布时间】:2019-12-09 09:46:09
【问题描述】:
我在将错误输出添加到 JWT 中间件设置时遇到问题。
我收到此错误:Cannot use object of type Slim\Http\Response as array
我正在使用 Slim 3 和 slim-jwt-auth 包,我正在使用在 https://github.com/tuupola/slim-jwt-auth#error 找到的文档中的示例代码
不同之处在于我调用的是\Slim\Middleware\JwtAuthentication 而不是Tuupola\Middleware\JwtAuthentication。如果我使用该类,则找不到该类。一切正常,直到我想将错误输出添加到中间件设置中,这是我的代码:
$app->add(new \Slim\Middleware\JwtAuthentication([
"path" => "/mypath",
"passthrough" => "/mypath/get-auth",
"secret" => getenv("SKEY"),
"secure" => false,
"error" => function ($response, $args) {
$data = array();
$data["status"] = "error";
$data["message"] = $args["message"];
return $response
->withHeader("Content-Type", "application/json")
->getBody()->write(
json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
]));
错误输出表明它来自$data["message"] = $args["message"];。
我是否正视问题而没有看到它?
【问题讨论】:
-
可能是因为
$args是Slim\Http\Response而不是array -
实际上不应该是“错误”的签名
"error" => function ($request, $response, $args) {}? -
@jDolba - 你是对的,这就是问题所在 - 谢谢