Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable scope when is set as view param and rendered by getRender() #12648

Closed
slechtic opened this issue Feb 24, 2017 · 17 comments
Closed

Variable scope when is set as view param and rendered by getRender() #12648

slechtic opened this issue Feb 24, 2017 · 17 comments
Labels
bug A bug report status: medium Medium
Milestone

Comments

@slechtic
Copy link

slechtic commented Feb 24, 2017

When I set view params and call getRender, the variables are visible in the view but out of their scope (in action) too.

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

    $view = new View();

    $view->setViewsDir($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'
    ));

    $view->setParamToView('paramFromService', 'Param from service');

    return $view;
}, true);

public function indexAction()
{
    $content = $this->view->getRender('index', 'index', [
        'viewParam' => 'Test view param'
    ]);
    var_dump($content);
    var_dump($viewParam);
    var_dump($paramFromService);
    die;
}
  • Variable $content is corrent.
  • Variable $viewParam contains string 'Test view param'. I expected notice Undefined variable.
  • Variable $paramFromService is assign to view in service.php and is accessible in this action too. I expected notice Undefined variable.

Testing environment

  • Phalcon version: 3.0.4
  • PHP Version: 5.6.24 as Apache module, 7.0.16. as Fcgi
  • Operating System: Windows 10
  • Server: Apache 2.4

Source files

https://github.com/slechtic/phalcon_variable_scope

@Jurigag
Copy link
Contributor

Jurigag commented Feb 27, 2017

Does this happen for 5.6.24 too? I thought it's only php 7 problem.

@slechtic
Copy link
Author

Yes, for both version of PHP.

@jesseforrest
Copy link

@sergeyklay : I was wondering if Bug - Medium refers to medium complexity or medium priority. If it's related to priority, I would like to push that this is high-priority for an MVC framework. imo, variable scope for views seems like an important topic for an MVC framework to get right.

Phalcon might be performing faster than it actually should be, since it likely is not be making copies of variables for each view, which may be required to resolve this problem. Either way, I just want to voice that I think this one is larger than it might seem (in complexity and priority), in order to do it right where it doesn't add a lot of overhead.

@Jurigag
Copy link
Contributor

Jurigag commented Mar 15, 2017

Well tbh as far as i see this https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/view/engine/volt.zep#L114

The current symbol table is method i think here. This is causing this problem. I guess the workaround could be something like create anonymous function which will just accept parameters and create it and do render there?

@jesseforrest
Copy link

I should also note, that I am not using Volt. I am using vanilla phtml. I'm assuming the code may be similar, but wanted to note that this is not just a Volt issue.

@Jurigag
Copy link
Contributor

Jurigag commented Mar 16, 2017

Yes - code is similar, i will try to work on it, and if my assumption will be correct i will do PR with fix.

@Jurigag
Copy link
Contributor

Jurigag commented Mar 17, 2017

This issue is fixed on php 5, but still happens on php 7.

On php 7 create_symbol_table isn't implemented at all in zephir.

@Jurigag
Copy link
Contributor

Jurigag commented Mar 24, 2017

Can we add here label php7? @sergeyklay

@sergeyklay
Copy link
Contributor

Fixed in the 3.2.x branch.

@sergeyklay sergeyklay added this to the 3.2.0 milestone Apr 9, 2017
piit79 pushed a commit to ca-asm/cphalcon that referenced this issue Jan 30, 2018
piit79 pushed a commit to piit79/cphalcon that referenced this issue Jan 30, 2018
piit79 pushed a commit to piit79/cphalcon that referenced this issue Jan 30, 2018
piit79 pushed a commit to piit79/cphalcon that referenced this issue Jan 30, 2018
piit79 pushed a commit to piit79/cphalcon that referenced this issue Jan 30, 2018
piit79 added a commit to piit79/cphalcon that referenced this issue Jan 30, 2018
piit79 added a commit to piit79/cphalcon that referenced this issue Jan 30, 2018
@piit79
Copy link

piit79 commented Jan 30, 2018

@sergeyklay Unfortunately, it seems this issue has not actually been fixed in PHP 7 and is still present in 3.3.1.
The unit test for this issue is incorrect and doesn't catch the failure - see PR #13288

I couldn't find the fix for this issue in the PR #12782 referenced above - is it possible that a commit got lost somehow?

@sergeyklay
Copy link
Contributor

@piit79 It is very strange.. I'll try to sort out as soon as I can. Let's use #13288 for discussion

@piit79
Copy link

piit79 commented Feb 8, 2018

@sergeyklay Thanks a lot for your attention to this issue. This has bitten us when we moved from PHP 5 to PHP 7. Once confirmed, wouldn't it make sense to reopen this issue?

@sergeyklay
Copy link
Contributor

I hope we managed to sort out with this issue finally here #13606. All necessary changes are already in the appropriate branch. We will try to release the next version in the coming days. Thank you for your patience, the report, and for helping us make Phalcon better.

@piit79
Copy link

piit79 commented Nov 21, 2018

@sergeyklay Just wanted to point out that the test for this failure condition still seems to be broken, unless it was fixed somewhere else and I missed it:

expect($e->getMessage())->contains("Undefined variable: a_cool_var");

My pull request #13288 with the fix was closed without merging for some reason...

@sergeyklay
Copy link
Contributor

@piit79 Your branch had conflicts so I couldn't merge it automatically. I merged these changes by hand into 3.4.x branch. The 3.3.x branch is no longer supported. We'll try to release 3.4.x ASAP.

@sergeyklay
Copy link
Contributor

try {
echo $a_cool_var;
$this->fail('Variable a_cool_var is defined and is set to "' . $a_cool_var . '"');
} catch (\PHPUnit_Framework_Exception $e) {
expect($e->getMessage())->contains("Undefined variable: a_cool_var");
}

@piit79
Copy link

piit79 commented Nov 21, 2018

Perfect, thanks! I was looking in the wrong place then. And sorry for the double post :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
None yet
Development

No branches or pull requests

7 participants