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.

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?



5.4k

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



20.6k

I'm afraid that's not the problem. The initialization is correct according to the documentation. Please check http://docs.phalconphp.com/pt/latest/api/Phalcon%5Mvc%5View



20.6k

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



5.4k

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 http://docs.phalconphp.com/en/latest/api/Phalcon%5Mvc%5View%5Engine%5Volt.html.



20.6k

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?



5.4k

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



20.6k

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.



5.4k

first variant works too.



5.4k
edited Oct '14

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



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