Having a bear of a time figuring out how to inject dependencies into Middleware. My dependencies file looks like this:
return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
App::class => function (ContainerInterface $container) {
AppFactory::setContainer($container);
return AppFactory::create();
},
Auth::class => function (ContainerInterface $container) {
return new Auth($container->get(MysqliDb::class));
},
. . . . . unrelated stuff . . . . .
AuthMiddleware::class => function (ContainerInterface $container) {
return new AuthMiddleware(
$container->get(Auth::class)
);
},
]);
};
then my Middleware like so:
class AuthMiddleware implements MiddlewareInterface {
/**
* @var Auth
*/
protected $auth;
/**
* @param Auth $auth
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
public function process(Request $request, RequestHandler $handler): Response
{
// some stuff using auth...
return $handler->handle($request);
}
}
but when I add it to a route:
$app->get('/members', MembersAction::class)->add(AuthMiddleware::class);
I keep getting the error:
{
"statusCode": 500,
"error": {
"type": "SERVER_ERROR",
"description": "Callable AuthMiddleware does not exist"
}
}
any clues where I’m going wrong? A kick in the right direction?