user1032531 user1032531 - 6 months ago 50
PHP Question

Set Slim container within middleware

How do I set something in Slim's container within middleware? Below is what I tried.

$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
$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");
return $logger;


Should be

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

as the container is bound to the callable.