2

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.

Veselin Bakov
  • 103
  • 1
  • 11
  • Did you set up view component correctly? This is my views component registered in services: http://pastebin.com/SdX121d8. Also make sure that Phalcon has r/w permission for cache folder. Check PHP logs – Luke Sep 07 '16 at 08:30
  • My guess is that `setViewsDir(__DIR__ . '/../views/')` does not set the correct path. Where do you register your View service? In the index.php file? If views are in `/app/views/` and your `index` file is in `/public/index.php` the path should be `__DIR__ . '../app/views/'`. – Nikolay Mihaylov Sep 07 '16 at 08:35
  • Yep, something in the definition of my Volt engine seems fishy, I added the phtml line from Luke's example and, yes, the .phtml template is loaded properly. Thanks, I'll report back when there's any development (both positive or if I get stuck again). – Veselin Bakov Sep 07 '16 at 08:39
  • Nice to hear it. I'll add my sample code as an answer so somebody could find the answer in case of pastebin being down. – Luke Sep 07 '16 at 09:05

1 Answers1

1

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

Luke
  • 2,350
  • 6
  • 26
  • 41