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

Cant find dependency injection container in Controller?

Hello Phalcon community,

I have been trying to acces a service thru the dependency injection container. What im trying to do is access a class called "Elements". This will hold my common elements. But now i want to access the DI, run a method thru the DI and get a string from that ( Page title ). Now the problem is that the controller does not seem to be able to resolve the DI.

This is my code im using to register my Elements class ( in the bootstrap ):

$di->set('elements', function() use($config){

$element = new Elements(); $element->SetSiteName( $config->web->sitename ); return $element; });

And here is how i utilize it in the controller:

class IndexController extends Controller

{ public function indexAction() { $elements = $this->di->get("elements"); $this->view->content = $elements->getTitle(); } }

For what ever reason my DI keeps being null. And the view is also empty

Full source code:

Using PHP 5.6.3, x86 Windows Xampp Phalcon version 2.0.2 (x86)

Is $this->getDI()->get("elements") work?

edited Jun '15

Is $this->getDI()->get("elements") work?

No it does not

These three wont work:




They all give null back.


Found the problem, one of my closures were not returining anything.