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

Problem registering view engines using class names. Is this a bug?

Hi, if we initialize two view services and register the view engines using the class name something goes wrong and we get not output. But if we return an instance of the service, then it works.

If we do like this, it doesn't work:

$di->set('view', function () use ($config) {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array(
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php',
        '.volt' => 'Phalcon\Mvc\View\Engine\Volt'
    ));
    return $view;
}, true);

$di->set('simpleView', function() use ($config) {
    $view = new Phalcon\Mvc\View\Simple();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array(
        ".phtml" => "Phalcon\Mvc\View\Engine\Php",
        ".volt" => "Phalcon\Mvc\View\Engine\Volt"
    ));
    return $view;
});

More specifically, it's doesn't work when the engines are registered using the class name in BOTH services. If one uses classnames and the other doesn't, then it works. This works all the time:

$di->set('view', function () use ($config) {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array(       
        '.phtml' => function ($view, $di) use ($config) {
            $phtml = new \Phalcon\Mvc\View\Engine\Php($view, $di);
            return $phtml;
        },      
        '.volt' => function ($view, $di) use ($config) {
            $volt = new  \Phalcon\Mvc\View\Engine\Volt($view, $di);           
            return $volt;
        }
    ));
    return $view;
}, true);

$di->set('simpleView', function() use ($config) {
    $view = new Phalcon\Mvc\View\Simple();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array(       
        '.phtml' => function ($view, $di) use ($config) {
            $phtml = new \Phalcon\Mvc\View\Engine\Php($view, $di);
            return $phtml;
        },      
        '.volt' => function ($view, $di) use ($config) {
            $volt = new  \Phalcon\Mvc\View\Engine\Volt($view, $di);           
            return $volt;
        }
    ));
    return $view;
});

Any ideas?



10.0k

Because \Phalcon\Mvc\View\Engine\Volt required 2 parameters.



32.2k

I'm afraid that's not the problem. The initialization is correct according to the documentation. Please check https://docs.phalcon.io/pt/latest/api/Phalcon_Mvc_View



32.2k

Could you give a more concrete and explicitly answer to the problem I presented please?



10.0k

Above in your code, you use \Phalcon\Mvc\View\Engine\Volt(required 2 parameters), NOT \Phalcon\Mvc\View(optional parameters). When you create instance of \Phalcon\Mvc\View\Engine\Volt without parameters, it will give error.You must use \Phalcon\Mvc\View\Engine\Volt with 2 parameters https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_View_Engine_Volt.html.



32.2k

Please show me where I fail to initialize the Volt engine because I can't see it. Are you refering to the line where I use the class name?

'.volt' => 'Phalcon\Mvc\View\Engine\Volt'

If yes, I'm just following the manual and it actually works! The problem, as I see it, is the interaction of the two view components. If I have only ONE view component it works, with TWO it doesn't. Did you try to replicate the problem?

@phalcon, any input to give?



10.0k

It's not problem, you merely used wrong code. You must use 2 variant, i tried run you code (2 variant), it's working. https://stackoverflow.com/questions/19122903/phalcon-view-engines



32.2k

I must be dumb because I still can't grasp what you are trying to explain. I know the second way I showed works, but why the first way of doing it doesn't (when using class names)? According to the documentation it should.



10.0k

first variant works too.



10.0k
edited Oct '14

Maybe i don't understand question correctly. Something wrong when you register services? Or when you try get access to them



98.9k
Accepted
answer

I think this happen because the engine instance is built using the dependency injector (also the instance is requested being kept by the DI):

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/view.zep#L577

This might cause both different view components to reuse the same instance of the engines possibly causing incompatibilities.

In the other case, using a closure will create a new instance of the engine everytime the service is built.