We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 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;
$View->registerEngines([
    '.phtml'=>function($simpleView,$DI){
        $Volt = new \Phalcon\Mvc\View\Engine\Volt($simpleView,$DI);
        $Volt->setOptions([
            'compiledPath'=>'/tmp',
            'compileAlways'=>TRUE
        ]);
        return $Volt;
    }
]);
$View->setViewsDir($aia_path.'/app/view/');

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.



125.7k
Accepted
answer

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.