Slim 3中间件设置参数没有得到

时间:2018-01-12 07:36:14

标签: php slim

settings.php文件代码

with open("test.txt") as f:
    lines = f.readlines()

new_lines = [line.replace("interface Vlan101", "vrf forwarding ABC" for line in lines]

with open("out.txt", "w") as f1:
    f1.writelines(new_lines)

index.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',
    ],
],];

我收到错误: 在不在

中的对象上下文中时使用$ this

如何在中间件中获取设置属性?

1 个答案:

答案 0 :(得分:2)

在这个上下文中,$ this应该是Slim Container对象。

设置存储为数组,可以这样访问:

$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);
});