Dependency injection in PHP (slim, php-di)

php-di/slim-bridge
slim dependency injection
slim framework
slim 4 dependency injection
php-di make
php di module
slim framework di container
slim 4 middleware

I have a Slim Php (slim4) application to which I added Monolog for logging purposes. I'm adding the logger to the application like this:

$containerBuilder->addDefinitions([
  LoggerInterface::class => function (ContainerInterface $c) {
     $logger = new Logger('appname');
     ...
     return $logger

This works fine for injecting the logger in most of my classes, by just doing:

public function __construct(ContainerInterface $container = null, LoggerInterface $logger)
{
    // I can use $logger here

Now I'd also like to use the logger in the middleware like authentication. I don't see how I can properly do this. I can get this working by adding the logger as a named entry in the container like this:

$containerBuilder->addDefinitions([
  "LoggerInterface" => function (ContainerInterface $c) {

and then passing it to the middleware as a constructor parameter by getting it back from the container:

$middlewares[] = new MyAuthentication(..., $container->get('LoggerInterface'));

But this:

  • a) breaks the injection by classname for the other classes
  • b) is apparently not a best practice

So what is the correct way to get this logger injected in the middleware?


Without adding the LoggerInterface as a named entry to the container, could you just inject the LoggerInterface class directly to your middleware via $container->get()? I.E. in a routes.php App function:

$container = $app->getContainer();
$app->add(new MiddleWare\MyAuthentication(..., $container->get(LoggerInterface::class)));

Slim - PHP-DI, Contribute to PHP-DI/Slim-Bridge development by creating an account on GitHub . Dependencies can then be injected in your controller using autowiring,� Dependency injection in PHP (slim, php-di) Ask Question Asked 1 month ago. Active 30 days ago. Viewed 72 times 4. 2. I have a Slim Php (slim4) application to which I


In short, I do not think you are going to be able to auto-wire the dependencies for the middleware as they need to be constructed before being appended to the router. You would need to explicitly inject the dependency as suggested by @Woodrow, although I would opt for method injection instead of constructor injection for the LoggerInterface as it would adhere to the LoggerAwareInterface.

PHP-DI/Slim-Bridge: PHP-DI integration with the Slim , Dependency Container. Slim uses a dependency container to prepare, manage, and inject application dependencies. Slim supports containers that implement PSR-11 or the Container-Interop interface. You can use Slim's built-in container (based on Pimple) or third-party containers like Acclimate or PHP-DI. In software engineering, dependency injection is a software design pattern that implements inversion of control for software libraries. Caller delegates to an external framework the control flow of discovering and importing a service or software module specified or "injected" by the caller.


You could let the dependency injection container (PHP-DI) resolve and inject all dependencies into your middleware:

LoggerInterface::class => function (ContainerInterface $container) {
    return new Logger('name');
},

Then just register the middleware with the class name:

$app->add(\MiddleWare\MyAuthentication::class);

(You should not inject the container itself into the middleware)

That's all.

Dependency Container, an optional dependency container to prepare, manage, and inject application dependencies. Slim supports containers that implement PSR-11 like PHP-DI. About. Dependency Injection in Slim 4. In contrast with Slim 2 and Slim 3, Slim 4does not ship with a DI container, but instead, supports any PSR-11compatibly DI container that you provide. This is part of Slim 4’s commitment to interoperability via the PHP-FIGstandards. The easiest way to add a container to your Slim application is to call AppFactory::setContainer()beforeyou call AppFactory::create().


Dependency Injection in Slim 4 – Rob Allen's DevNotes, Register the container with Slim. Let's look at how it works using PHP-DI as our container. To configure PHP-DI, we use the ContainerBuilder()� I'm trying to learn the Slim framework and PHP-DI at the same time. I don't want folks to write my code for me, but I'm at a complete stand-still here. All I want to do is figure out how to pass Slim's PHP-View object into a controller class using PHP-DI's Slim Bridge. From there, I'll mess about and figure things out.


Dependency injection in PHP (slim, php-di), Without adding the LoggerInterface as a named entry to the container, could you just inject the LoggerInterface class directly to your� PHP-DI is a dependency injection container meant to be practical, powerful, and framework-agnostic. Read more on the website: php-di.org. Get community support in the Gitter chat room: For Enterprise. Available as part of the Tidelift Subscription


Slim 4, PHP 7.2+; Composer; A Slim 4 application. Introduction. PHP-DI is one of the most used Dependency Injection Container for PHP. Dependency injection makes testing easier. The injection can be done through a constructor. A dependencies injection container (aka IoC Container) is a tool for injecting dependencies. A general rule: The core application should not use the container.