settings.php文件代码
return [
'settings' => [
'displayErrorDetails' => true, // set to false in production
'addContentLengthHeader' => false, // Allow the web server to send the content-length header
'encryption_key' => 'key1',
'jwt_secret' => 'secret1',
'db' => [
'servername' => 'localhost',
'username' => 'user',
'password' => 'pwd',
'dbname' => 'db',
],
],];
index.php文件代码
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
$app->add(new \Slim\Middleware\JwtAuthentication([
"path" => "/",
"passthrough" => ["/login"],
"secret" => $this->jwt_secret,
"secure" => false,
"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));
}]));
我收到了错误:在中没有在对象上下文中使用$this
如何在中间件中获得设置属性?
发布于 2018-01-12 13:43:29
在这种情况下,$this应该是对象。
这些设置存储为数组,可按以下方式访问:
$app->add(function (Request $request, Response $response, $next) {
/* @var Container $this */
$settings = $this->get('settings');
$jwtSecret = $settings['jwt_secret'];
// Do something...
return $next($request, $response, $next);
});
https://stackoverflow.com/questions/48221598
复制相似问题