在中间件中设置Slim容器

时间:2016-08-20 19:16:19

标签: php slim

如何在中间件中的Slim容器中设置内容?以下是我的尝试。

$app = new \Slim\App(); // Using Slim 3.0

$app->add(function ($request, $response, $next) {
    $this->logger->addInfo('$this'.print_r($this,1));  //This works, so I can access the container
    $this->app->getContainer()['xxx']='yyy';
    $next->getContainer()['foo']='bar';
    $this->bla=123;
    $this->logger->addInfo('$this'.print_r($this,1));
    $response = $next($request, $response);
    return $response;
});

$container = $app->getContainer();

$container['logger'] = function() {
    $logger = new \Monolog\Logger('my_logger');
    $file_handler = new \Monolog\Handler\StreamHandler("../logs/app.log");
    $logger->pushHandler($file_handler);
    return $logger;
};

1 个答案:

答案 0 :(得分:1)

$this->app->getContainer()['xxx']='yyy';

应该是

$this['xxx'] = 'yyy';

因容器绑定到callable。