cphalcon: [BUG]: Passing variables from the controller to the view does not work.

Describe the bug There seems to be a communication problem between the controller and the view that causes the variables not to be defined in the view.

This problem happens since version 4.0.0-rc.2+4138

To Reproduce

use Phalcon\Mvc\Controller;

class IndexController extends Controller {
    
    public function indexAction() {
		
	$this->view->setVar('test', 'value'); // This does not work in view
	$this->view->disable(); // This does not work.
	echo $this->view->getVar('test'); // This works in controller.
		
    }
	
}

Details

  • Phalcon version: 4.0.0-rc.2+4237 x64 nts
  • PHP Version: PHP 7.3.10 x64 nts
  • Operating System: Windows 10 x64
  • Server: Apache

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 22 (19 by maintainers)

Commits related to this issue

Most upvoted comments

@Jeckerson I have the following

// view service (fpm)
$view = new \Phalcon\Mvc\View();

$view->setViewsDir(__DIR__ . '/views/');
$view->setPartialsDir('_partials/');
$view->setLayoutsDir('_layouts/');

$di->set('view', $view');       // Doesn't work

$di->setShared('view', $view);  // But it works

Please make it so $di->set() works too. Why do we have to use $di->setShared()? This should be a default! Thank you!

It is in the future plans after v4 is released. We intend on releasing a new container fully PSR-11 compatible and with auto-wiring.

We cannot change this behavior now because it will pretty much break every single application there is out there that is to be upgraded to v4. By releasing a new container we give developers ample time to adjust their applications.

I agree with @Jurigag and @alexbusu The way the di was designed is indeed not optimal and misleading in cases. The get should always give me the same shared object and if I want a brand new one I can use factory/fresh to get that new object. This is the way psr-11 containers operate also.

I think once we release a full psr-11 container things will start becoming a bit easier. For now we just have to work with what we have. ๐Ÿ˜•

To be honest i would consider if this is really a bug. setShared is expected to return the same instance of a service while set not. $di->set('view', $view); actually works because we created instance of class earlier and we are setting the same instance to view. However using closure we are creating new instance each time, causing set to return new each time as expected.

In comparison of how it worked previously, yea, itโ€™s a bug, but was this previous behavior correct?

@ruudboon Added descriptions