我发现访问代码中各处的应用程序设置很有用。所以我把容器
// array passed to DI\ContainerBuilder−>addDefinition()
return [
...
'settings' => function () {
return require __DIR__ . '/settings.php';
},
...
];
我的问题是:我怎样才能访问请求(Psr\Http\Message\RequestInterface)“在我的代码中的每一个地方”?使用相同的机制还是可能有一些我错过的更简单的东西?
==== 更新 ====
为了更准确地按照 Nima 的要求,我喜欢 Slim 处理错误的方式(http://www.slimframework.com/docs/v4/middleware/error-handling.html),所以我经常使用它!
use Slim\Exception\HttpForbiddenException;
...
// security issue for example, somewhere deeeeeeeep in the code
if ($this_is_denied)
throw new HttpForbiddenException($request, 'no way !');
那么 Slim\Exception 需要 '$request' 作为参数。这就是我问题的重点...
====结论====
多亏了 Nima,这是一种不好的做法:/(参见下面的评论),所以算了吧!亲切的问候