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

Di container Not Available for VOLT

php7.2 been having some trouble with the dependency injector not available in phalcon 4

The app sets up no problem. I set the view using:

     $di = new FactoryDefault();

     //otherservices defined here

    $di->setShared('view', function() use($di){
        $view = new View();

       $view->registerEngines(array(".phtml" => function($view) use($di){

           $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);

           $volt->setOptions(array(
                "compiledPath" => "../some/cache/volt/",

           ));

           return $volt;
    }));

        $view->setViewsDir('../some/dir/');
        return $view;
    });

When this code is run, everything fires smoothly till we get to:

{{ assets.outputCss('fonts') }}

At which point the server says:

PHP Fatal error: Uncaught Error: Call to a member function outputCss() on

So i tried to see what was going on and basically:

$this->view->assets = $this->assets

Gets the assets loaded.

But then when I get down the page to:

<input type="hidden" id="signup-csrf-token" name="{{ security.getTokenKey() }}" value="{{ security.getToken() }}">

PHP Fatal error: Uncaught Error: Call to a member function getTokenKey()

security is defined in the $di as

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

    $security = new Security();

    //Set the password hashing factor to 12 rounds
    $security->setWorkFactor(12);

    return $security;

    }, true);

Been at this for about 3 hours now and cannot find anything in the documentation to help. if you have any ideas at all that would be great. I know phalcon 4 is not quite ready but we are migrating already because we don’t want to be rushing last minute to make all the necessary changes.

If any one is struggling with this a work around is to do:

    $this->view->assets     =   $this->di->get("assets");
        $this->view->security   =   $this->di->get("security");

this causes the controller to refetch and reassign the data and you can access as normal in the volt view. If I find out why the $di container is not avilable in $view I will update again.

I noticed you didn't specify in your question that you're making a "fonts" collection - but I assume that was just a conscious omission.

Have you tried this code using v3.4? It looks like 4.0 and 3.4 are identical in this sense, so if it works in 3.4, but not 4.0 I'd suggest posting a similar question on GitHub. If it doesn't work in 3.4, then it's almost definitely your code - though I can't immediately see any problem.

If that ends up being the case, please post your complete bootstrap code (at least the part concerning the view), your controller method, and your Volt template file.