cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Passing variable to view not working with latest Phalcon 2.1.x build and PHP7.

Open Tosyn opened this issue 10 years ago • 5 comments

Passing variable to view via controller is not working (getting variable not defined error message). Am I doing something wrong or the parsers (PHQL, Volt, Annotations) still need to be migrated to PHP7 extensions as pointed out here https://forum.phalconphp.com/discussion/9715/php-7-can-you-please-update-us-on-the-progress ?

Tosyn avatar Apr 01 '16 08:04 Tosyn

ref https://github.com/phalcon/cphalcon/issues/10378#issuecomment-198308702

Green-Cat avatar Apr 01 '16 08:04 Green-Cat

There are many problems with phaclon and php 7, it's not stable yet. Check issue above.

Jurigag avatar Apr 01 '16 08:04 Jurigag

Controller:

class IndexController extends ControllerBase
{
    public function indexAction()
    {
        $this->view->title = 'my page';
    }
}

View:

<h1>Congratulations {{title}}!</h1>

<p>You're now flying with Phalcon. Great things are about to happen!</p>

config/services.php

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

    $view = new View();

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

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ));

            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));

    return $view;
}, true);

Error: "PHP message: PHP Notice: Undefined variable: title in /path-to-app_views_index_index.volt.php on line 1

Tosyn avatar Apr 01 '16 08:04 Tosyn

First:

<h1>Congratulations {{title}}!</h1>

On php7 there is some bug and you have to use view.title, i guess there is some kind of problem with https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/view.zep#L1348

But also setVar and etc methods doesn't work so i don't know what's going on.

Second: '.phtml' => 'Phalcon\Mvc\View\Engine\Php' - this doesn't gonna work, zephir has some problems with creating class instances when it's provided as string, just use functions returning objects.

Maybe if we define it - https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/view.zep#L107 as empty array it would start working ?

Jurigag avatar Apr 01 '16 08:04 Jurigag

@jurigag: thanks, using view.title as suggested worked. I will comment out the .phtml part for now. I will run some more test and close the issue shortly if I am not able to find any related issue.

Tosyn avatar Apr 01 '16 08:04 Tosyn