在中间件中设置Slim容器

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
    $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个回答

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

Should be

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

as the container is bound to the callable.

dqwh1209
dqwh1209 谢谢你geggleto。 是的,经过更多的比赛,我期待如此,那$ this-> bla = 123; 也会有用。 我之前做的事情是“错误的”,这使得它不那么明显。
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐