Reputation: 13
I want to configure several middleware in Dependency Container in Slim, so that I can set several constants in a same place and add middleware in a ease.
E.G.
$configuration = [
'settings' => [
'displayErrorDetails' => true,
],
'auth_settings' => [
'serect' => 'garyAPIserver',
],
];
$container = new Slim\Container($configuration);
$container['auth'] = function ($c) {
return new AuthMiddleware($c['auth_settings']);
};
$app = new Slim\App($container);
And I try to invoke the middleware in DI:
$app->add($app->get('auth'));
And I got the warning message print by php:
Warning: Missing argument 2 for Slim\App::get(), called in E:\www\slimServer-3.0\index.php on line 12 and defined in E:\www\slimServer-3.0\vendor\slim\slim\Slim\App.php on line 146
And the error message print by Slim:
Type: RuntimeException
Message: is not resolvable
File: E:\www\slimServer-3.0\vendor\slim\slim\Slim\CallableResolver.php
Line: 82
I am new in Slim, there it possible to set middleware in DI? Is there any guides with the similar scenario?
Upvotes: 0
Views: 1655
Reputation: 8738
You can do it using the $container
variable:
$app->add($container->get('auth'));
Then you can use it in your router functions using:
$auth = $this->get('auth');
Take a look here for more information.
Upvotes: 1