We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

How to configure standalone simple view

I have a single page app (smaller in functionality than even a micro app). This app is using a view file defined elsewhere to generate some output. How do I go about setting up a standalone SimpleView component to generate the HTML from my view?

This is what I have now:

$DI = new \Phalcon\DI\FactoryDefault();
$View = new \Phalcon\Mvc\View\Simple;
        $Volt = new \Phalcon\Mvc\View\Engine\Volt($simpleView,$DI);
        return $Volt;

echo $View->render('query/index',['Response'=>$Response]);

Which is giving me a Fatal Error:

Uncaught Phalcon\Mvc\View\Exception: A dependency injector container is required to obtain the application services in phalcon/mvc/view/simple.zep:159

But I thought I was passing the DI everywhere it needed to be...

edited Oct '19

Try this:

new \Phalcon\Di\FactoryDefault();
$DI =  \Phalcon\Di::getDefault();

Try this:

new \Phalcon\Di\FactoryDefault();
$DI =  \Phalcon\Di::getDefault();

No change.


So I fixed the problem by going all-in with the DI. I initialized it, defined a "volt" service, defined a "view" service that then used the "volt" service, then used $DI->get('view')->render(...) to generate my output.