phpphalconvoltphalcon-routing

Phalcon view not loaded


I'm trying to load a template in PhalconPHP (v 2.0.13), but even the simplest example doesn't seem to work. I'm trying to access http://www.mysite.dev.fb/forms/ in this example. Here is my router:

$router->add(
    '/forms/',
    [
            "namespace" => "Render\\Controller",
            "controller" => "index",
            "action" => "forms",
    ],
    ['GET']

);

The routing works, or at least the code in the action is reached (vardumps, etc).

Here's my Controller action variants.

Variant 1

public function formsAction()
{
}

In this case, the template located at app/views/index/forms.volt (I have a copy of the file with an .phtml extension, for debugging purposes) should be loaded, right? Wrong, an empty screen is displayed, no errors in errorlog.

Variant 2

Then, I tried picking the view, like that:

$this->view->setViewsDir(__DIR__ . '/../views/');
$this->view->pick('forms/contact');

The file, app/views/forms/contact.volt, also exists, with full permissions. Vardumping $this->view->getContent() returns null and the result is again an empty white screen without any errors.

Variant 3

Desperately, I tried directly rendering the template (for this example I'm using the default Phalcon index/index template) like this:

$this->view->start();
$this->view->render('index', 'index'); //Pass a controller/action as parameters if required
$this->view->finish();

The only difference is that now vardumping $this->view->getContent() returns an empty string, instead of null.

It's like automatic rendering is disabled, but the following line returns false (as it should):

var_dump($this->view->isDisabled());

I'm out of ideas, can anyone help? If I forgot to include something, respond and I'll include it.


Solution

  • You should definitely check your PHP logs for PHP errors. Also I suspect that your volt declaration might be wrong. Here is a working example of declaring a dependency injectable view component in services that I use:

    $di->setShared('view', function () use ($di,$config) {
        $view = new View();
        $view->setViewsDir($config->application->viewsDir); // path to directory with views, loaded from config in this case   
        $view->registerEngines(array(
            '.volt' => function ($view, $di) use ($di, $config) {
                $volt = new VoltEngine($view, $di); 
                $volt->setOptions(array(
                    'compiledPath' => $config->application->cacheDir, // path to cache dir, loaded from config in this case
                    'compiledSeparator' => '_'
                ));          
                return $volt;
            },
            '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
        ));
        return $view;
    });
    

    Ensure that your webserver has rights to read views and R/W rights for cache directory. Hope it will help you