I have a logger class (Application\Debug\Logger) for logging various debug messages from whole application to log files. And I would like to get instance of that class using simple alias 'logger'. So I try to configure Zend\Di\Di, so that I could do something like this in, for example, module controller or another application components:
$di = new \Zend\Di\Di();
$logger = $di->get('logger');
//$logger should be Application\Debug\Logger instance...
To achieve this, I have this in Application/config/module.config.php:
'di' => array(
'instance' => array(
'alias' => array(
'logger' => 'Application\Debug\Logger',
),
),
)
But the problem is: alias 'logger' doesn`t work. I can use Di and get class by full name (Application\Debug\Logger), but not by alias (I get error: Class logger could not be located in provided definitions). I suppose that I have to pass the configuration to Di somehow, but I don't know how. Can you help me?
I`ve found partial solution of my problem. I noticed that when I call ServiceManager, it uses also Dependency Injection configuration to find proper class, so I can call:
in the controller to get logger instance by alias configured for DI.
So in my config file I could also put:
and it works the same as previous di config example. But I still don`t fully understand how I can properly use DI, so I would be grateful if someone could explain it to me.