有没有办法在Zend Framework 2中设置主体?在Zend Framework 1中我可以这样做
$this->getResponse()->setBody('Hello World')
我找到了一种在Zend Framework 2中设置内容的方法,但这也会覆盖布局,这不是我想要的。
答案 0 :(得分:0)
这是另一种方法......
<?PHP
namespace ModuleName\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
protected $helloWorldTable;
public function indexAction()
{
return new ViewModel(array(
'foo' => 'Hello World From Zend Framework 2!',
));
}
}
<?PHP
$this->headTitle('Some Site...');
echo "$hello";
?>
<html lang="en">
<head>
<meta charset="utf-8">
<?php echo $this->headMeta()->appendName('viewport', 'width=device-width, initial-scale=1.0') ?>
<!-- Le styles -->
<?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/images/favicon.ico')) ?>
<!-- Scripts -->
<?php echo $this->headScript()->prependFile($this->basePath() . '/js/html5.js', 'text/javascript', array('conditional' => 'lt IE 9',))
->prependFile($this->basePath() . '/js/bootstrap.min.js')
->prependFile($this->basePath() . '/js/jquery.min.js') ?>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="<?php echo $this->url('index') ?>"><?php echo $this->translate('Spamus') ?></a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="<?php echo $this->url('index') ?>"><?php echo $this->translate('Home') ?></a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
<?php echo $this->content; ?>
<hr>
<footer>
<p>© 2012 - 2013 by YourSite <?php echo $this->translate('All rights reserved.') ?></p>
</footer>
</div> <!-- /container -->
<?php echo $this->inlineScript() ?>
</body>
</html>
答案 1 :(得分:-1)
无论你改变什么,控制器中的响应体,当zf2 MvcEvent :: EVENT_RENDER触发时,将重建一个新的Response体。所以正确的方法是改变响应体 AFTER MvcEvent :: EVENT_RENDER。
将此添加到您的控制器:
$this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER, function($event){
$event->getResponse()->setContent('foobar');
}, -10000);
答案 2 :(得分:-1)
我终于得到了解决方案。要更改布局中的内容,只需键入控制器的操作
即可$this->layout()->content = 'foobar';