Closed jasny closed 1 year ago
Hi, it seems you missed this part from the docs:
Is it still relevant? If Slim 4 does that automatically we can consider it, but I think the bridge is still relevant here.
@mnapoli As I tested, DI in controllers is working fine with php-di itself, without Slim-Bridge. Am I missing some point why the documentation references it as an additional feature?
For reference, I've created a service class like this:
class TestService {
public function getPongMessage() {
return 'Pong';
}
}
Then I've created controller like this:
class PingRoute
{
private TestService $testService;
public function __construct(TestService $testService) {
$this->testService = $testService;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args)
{
$message = $this->testService->getPongMessage();
$result = new ResponseWrapperDto(new MessageDto($message));
$response->getBody()->write(json_encode($result));
return $response->withHeader('Content-type', 'application/json');
}
}
Here's how I connected the controller:
$app->get('/ping', PingRoute::class);
Here's how I instantiate my slim app with php-di:
$containerBuilder = new ContainerBuilder();
$container = $containerBuilder->build();
AppFactory::setContainer($container);
$app = AppFactory::create();
I assume DI works well with this setup based on a successful response.
You haven't tested all of this bridge's features. I'll keep this issue closed.
@mnapoli you are absolutely right. Sorry for being off-topic. My point is documentation https://php-di.org/doc/frameworks/slim.html may contain some misleading information. Feature "controllers as services, allowing dependency injection in controllers" may require some clarification.
Slim v4 supports dependency injection using a container via
AppFactory::setContainer($container)
. See documentation.This project can be archived and the PHP-DI documentation for Slim can be updated to match the Slim documentation.