-1
如何在中間件內的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;
};
謝謝geggleto。是的,經過更多的遊戲後,我期望如此,而且'$ this-> bla = 123;'也可以。我之前做的事情是「錯誤的」,這使得它不那麼明顯。 – user1032531