We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

injectionAwareInterface auto inject not working?

DI not getting auto injected on class implimenting injectionAwareInterface?

I've implimented the method, then when I call the class, the di is null, is there anything else needed for it to auto inject itself?



12.6k
Accepted
answer

Ok, I see how it's done, the DI will only autoinject itself, if you call the class from the DI,

For anyone else who is interested, means you need to define your class as a service and resolve it with DI instead of manually,

i.e.

$bannerModule = new BannerModule(); //  This will not have the DI injected to it

$di->set('bannerModule', function() {
    return new BannerModule(); 
});

$di->get('bannerModule'); // This will have the DI injected to it

Hope this helps someone else too.