Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 23d ago

Try this:

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

Try this:

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

No change.



95.4k
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.