我想在Slim的Dependency Container中配置几个中间件,这样我就可以在同一个地方设置几个常量,并轻松添加中间件。
例如
$configuration = [
'settings' => [
'displayErrorDetails' => true,
],
'auth_settings' => [
'serect' => 'garyAPIserver',
],
];
$container = new SlimContainer($configuration);
$container['auth'] = function ($c) {
return new AuthMiddleware($c['auth_settings']);
};
$app = new SlimApp($container);
我尝试调用DI:中的中间件
$app->add($app->get('auth'));
我得到了php打印的警告消息:
警告:Slim\App::get()缺少参数2,在第12行的E:\www\slimServer-3.0\index.php中调用,并在第146 行的E:\www\simServer-3.0\vendor\Slim\Slim\App.php中定义
Slim打印的错误消息:
Type: RuntimeException
Message: is not resolvable
File: E:wwwslimServer-3.0vendorslimslimSlimCallableResolver.php
Line: 82
我是Slim的新手,有可能在DI中设置中间件吗?有类似场景的指南吗?
您可以使用$container
变量:
$app->add($container->get('auth'));
然后你可以在你的路由器功能中使用它,使用:
$auth = $this->get('auth');
查看此处了解更多信息。