zend-framework2 - ZF2。我是否总是需要从操作中返回 ViewModel?

标签 zend-framework2

从现在开始 - 我的每个操作都必须有一些返回的 ViewModel,例如:

$viewModel = new ViewModel();
return $viewModel;

但是,如果在某些情况下我只是像这样为布局的内容变量赋值怎么办:

$content = 'blah blah some content';
$this->layout()->setVariable('content', $content);  

所以 - AFAIk - 在这种情况下我真的不需要返回 ViewModel - 对吗? -as 布局的内容已经填充..但是 -如果我不这样做 - ZF2 会自动注入(inject)一个,并且我收到有关不存在模板的错误.....有关如何避免返回 ViewModel 的任何建议吗?

最佳答案

你可以

return $this->getResponse();

这将导致不需要返回任何 file.phtml。

关于zend-framework2 - ZF2。我是否总是需要从操作中返回 ViewModel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14441233/

相关文章:

php - Zend 框架 2 : Get the path to the root directory of the application

php - ZF2 - 将 Controller 名称获取到布局/ View 中

php - 在 Zend\Db\Sql\Select 之后获取 SQL

sql - ZF2插入多行

PHP:类名常量与字符串性能

php - 如何在 PHP 中将多个值从 View 传递到 Controller

javascript - 如何解决无法在家中重现的客户端Javascript错误?

php - 如何启动一个新的大型 ZF2 项目?

zend-framework2 - Zend FrameWork 2 Get ServiceLocator In Form 并填充下拉列表

layout - Zend Framework 2 - 布局和变量