user1032531 user1032531 - 1 year ago 181
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;

Answer Source

Should be

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

as the container is bound to the callable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download