As I see in the MVC Application, the Dispatcher creates a Controller using the Di. When I have been started to use the Micro I expected the same behavior with a Micro\Collection in the lazy mode.
As I understood the Handler instance is created by the \Phalcon\Mvc\Micro\LazyLoader at 49 line. The LazyLoader is used in the Micro where the Di is presented.
So I think it's possible to pass the Di as an argument to the LazyLoader constructor to make the new Handler instance.
@berrymore Could you please provide a minimal example which shows the issue?
@sergeyklay When you have composed your MVC Application, you can pass the controller definition to the DI.
// Somewhere in bootstrap.php
<?php
// ...
$di->setShared('App\\Controllers\\IndexController', [
'className' => 'App\\Controllers\\IndexController',
'arguments' => [
['type' => 'service', 'name' => 'router']
]
]);
<?php
// IndexController.php
public function __construct($router)
{
var_dump($router->getDefaults());
}
But if you'll do the similar in a Micro application.
$main->setHandler('App\\Controllers\\IndexController', true);
$app->mount($main);
You will get an Exception that IndexController cannot be instantiated because 0 arguments was specified instead of 1.
Because "lazy-handler" is created by this code with a simple new statement, while "MVC Controllers" are being created by Dispatcher using Di within this code
I think it is possible to pass the DI from Micro to LazyLoader class at this point to achieve the same behavior as in MVC Controllers.
Otherwise it should be refactored to give a developer an opportunity to specify a custom "resolver" instead of LazyLoader class.
(I fixed a typo in @berrymore's code, just in case anybody copies and pastes it into their project :wink: )
Closing this: Please vote for this feature here: https://github.com/phalcon/cphalcon/issues/14608
Most helpful comment
(I fixed a typo in @berrymore's code, just in case anybody copies and pastes it into their project :wink: )