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

$views->getContent() returns always ""

Using Volt or PHP template engine the $views->getContent() returns always "". The action and the controller are the right ones, but the content of the index.volt (or index.phtml) is ignored. No exception is raised. The viewsDir is also correct and all the routes are mounted. When i try to get the main site, the response content is empty. I tried also using the application object, same result. I tried with Volt and PHP template engine, same result. The dir structure is:

pippo
    boot
        init.php
        loader.php
        services.php
    controllers
    routes (defined via groups)
    views
        index
              index.phtml
              index.volt
        layout
        partials
        index.volt
        index.phtml

Here is the init.php file:

try {
  $config = new Phalcon\Config\Adapter\Ini(__DIR__.'/../config.ini');

  // System bootstrap.
  require __DIR__."/../boot/loader.php";
  require __DIR__."/../boot/services.php";

  // Request the services from the services container
  $router = $di['router'];

  $router->handle();

  $dispatcher = $di['dispatcher'];

  // Pass the processed router parameters to the dispatcher
  $dispatcher->setControllerName($router->getControllerName());
  $dispatcher->setActionName($router->getActionName());
  $dispatcher->setParams($router->getParams());

  // Dispatch the request
  $dispatcher->dispatch();

  $view = $di['view'];

  // Start the view
  $view->start();

  // Renders the related views.
  $view->render(
    $dispatcher->getControllerName(),
    $dispatcher->getActionName(),
    $dispatcher->getParams()
  );

  //$view->setContent("<h1>this works instead</h1>");

  // Finish the view
  $view->finish();

  $response = $di['response'];

  // Pass the output of the view to the response
  $response->setContent($view->getContent());

  // Send the request headers
  $response->sendHeaders();

  // Print the response
  echo $response->getContent();
}
catch (Phalcon\Exception $e) {
  echo $e->getMessage();
}

I have also tried to pick the right view in the indexAction method of the IndexController class, but still it ignores the phtml or volt files.

Are you setting the views directory?

$di['views'] = function(){
    $view = new Phalcon\Mvc\View();
    $view->setViewsDir('../views/');
    return $view;
};

Yes sure,

I use a config.ini file, then in the DI:

$di->setShared('view',
  function() use ($config) {
    $view = new View();

    $view->setViewsDir($config->application->viewsDir);
    $view->setLayoutsDir($config->application->layoutsDir);
    $view->setPartialsDir($config->application->partialsDir);

    $view->registerEngines(
      [
        '.volt' => 'volt',
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
      ]
    );

    return $view;
  }
);

I debugged and the dirs are ok, I can see them in the $view object.

Try setting up a events manager in the view component to see the exact path that cannot be loaded by the component:

$di['view'] = function() {

    //Create an events manager
    $eventsManager = new Phalcon\Events\Manager();

    //Attach a listener for type "view"
    $eventsManager->attach("view:notFoundView", function($event, $view) {
        throw new Exception($view->getActiveRenderPath());
    });

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

    $view->setViewsDir("../../views/");

    //Bind the eventsManager to the view component
    $view->setEventsManager($eventsManager);

    return $view;
};


24.4k
Accepted
answer

Yeah, my bad.

$view->setViewsDir($config->application->viewsDir)

should be:

$view->setViewsDir(_DIR_.$config->application->viewsDir);