I'm migrating a project from an older version of Slim and PHP-DI to the last one and PHP 8.1.17.
These are the composer dependency for slim and php-di:
"slim/slim" : "~4.11",
"php-di/php-di" : "^7.0.2",
PHP-DI annotation doesn't work like the previous one and I cannot understand why.
This is the main entry file:
use DI\ContainerBuilder;
$containerBuilder = new ContainerBuilder();
$containerBuilder->useAttributes(true);
$dependencies = require __DIR__ . '/../src/dependencies.php';
$dependencies($containerBuilder);
$app = $container->get(App::class);
(require __DIR__ . '/../src/middleware.php')($app);
$app->run();
This is the container bootstrap file (dependencies.php):
return function (ContainerBuilder $containerBuilder) {
//config
$containerBuilder->addDefinitions([
App::class => function (ContainerInterface $container) {
$app = AppFactory::createFromContainer($container);
return $app;
},
}
This is the middleware import file (middleware.php):
return function (App $app) {
$app->add($app->getContainer()->get(SystemMiddleware::class));
};
And this is the SystemMiddleware implementation:
final class SystemMiddleware implements MiddlewareInterface
{
#[Inject]
protected App $app;
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->app
//^^^^^^^^^ here app is not initialized!
return $handler->handle($request);
}
}
Currenctly I have to pass $app to SystemMiddleware constructor, but why #[Inject] doesn't work? What I'm missing?