如何在中间件中的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;
};
答案 0 :(得分:1)
$this->app->getContainer()['xxx']='yyy';
应该是
$this['xxx'] = 'yyy';
因容器绑定到callable。