cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

setDI for a view does not set iself as a 'view' in DI container

Open eugene-p opened this issue 13 years ago • 3 comments

Tried in version 0.7.0 Happens when Volt used separately from application

Setup:

  • index.php
  • views
    • index
      • index.html
    • layout
      • main.html

index.php

    $di = new Phalcon\DI\FactoryDefault();
    $di->set('volt', function ($view, $di){
         return new Phalcon\Mvc\View\Engine\Volt($view, $di);
    });

    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir('views/');
    $view->registerEngines(array(
        '.html' => 'volt'
    ));
    $view->setDi($di);                                                                                                                                                                                                                            
    $view->start();
    $view->render('index', 'index');
    $view->finish();
    echo $view->getContent();

views/index/index.html

<h1>Main</h1>                                                                                                                                                                                                                                 
{% block content %}
    Not index
{% endblock %}

views/index/index.html

{% extends "layouts/main.html" %}                                                                                                                                                                                
{% block content %}
<h2>Index</h2>
{% endblock %}

Would throw:

PHP Fatal error: Uncaught exception 'Phalcon\DI\Exception' with message 'Service 'view' wasn't found in the dependency injection container' in index.php:15 Stack trace:

0 [internal function]: Phalcon\DI->get('view')

1 [internal function]: Phalcon\Mvc\View\Engine\Volt\Compiler->_compileSource('{% extends "lay...')

2 [internal function]: Phalcon\Mvc\View\Engine\Volt\Compiler->compile('views/index/ind...', 'views/index/ind...')

3 [internal function]: Phalcon\Mvc\View\Engine\Volt->render('views/index/ind...', Array, true)

4 [internal function]: Phalcon\Mvc\View->_engineRender(Array, 'index/index', true, true, NULL)

5 /home/epatsiomkin/phalcon/index.php(15): Phalcon\Mvc\View->render('index', 'index')

6 {main} thrown in index.php on line 15

Though if you replace {% extends "layouts/main.html" %} with {% extends "views/layouts/main.html" %} in views/index/index.html Everything renders properly.

It also works if you add view to di $di->set('view', $view); with original templates but it does not feel right in this case. As I would expect view would register itself on setDi() call.

eugene-p avatar Dec 05 '12 17:12 eugene-p

Yauheni,

Try registering Volt this way:

//Register Volt as a service $di->set('volt', function($view, $di) { return new \Phalcon\Mvc\View\Engine\Volt($view, $di); });

//Register Volt as template engine

$di->set('view', function() {

$view = new \Phalcon\Mvc\View();

$view->setViewsDir('../app/views/');

$view->registerEngines(array(
    ".volt" => 'volt'
));

return $view;

});

You need to tell the DI container what service the view is using. In your implementation you are doing it right but not registering the view in the DI

niden avatar Dec 05 '12 19:12 niden

I do know how to set code up to make it work. My concerns are more with architecture

I have 2 concerns in current situation:

  1. View depends on Engine, Engine depends on View. It looks like a circular dependency. And does not sound like a good idea.
  2. Even if first part is not an issue, view should act same no matter how extend paths are specified. But it only tries to access view only if path relative to view root is specified.

So as a solution I was suggesting to register view automatically on setDi in dependency container. Now when I think of it, better solution might be to pass view root folder on Phalcon\Mvc\View->_engineRender() call

eugene-p avatar Dec 05 '12 23:12 eugene-p

Hi eugene, this was changed in 0.8.0, to avoid the circular dependency, thanks

ghost avatar Dec 19 '12 05:12 ghost