PHP-DI / Slim-Bridge

PHP-DI integration with the Slim framework
http://php-di.org/doc/frameworks/slim.html
MIT License
176 stars 38 forks source link

Bridge is no longer needed #75

Closed jasny closed 1 year ago

jasny commented 2 years ago

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.

mnapoli commented 2 years ago

Hi, it seems you missed this part from the docs:

image

Is it still relevant? If Slim 4 does that automatically we can consider it, but I think the bridge is still relevant here.

mr9d commented 1 year ago

@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.

mnapoli commented 1 year ago

You haven't tested all of this bridge's features. I'll keep this issue closed.

mr9d commented 1 year ago

@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.