0

有没有办法在 Zend Framework 2 中设置主体?在 Zend Framework 1 中,我可以这样做

$this->getResponse()->setBody('Hello World')

我找到了一种在 Zend Framework 2 中设置内容的方法,但这也覆盖了布局,这不是我想要的。

4

3 回答 3

0

这是另一种方法来做到这一点......

ModuleName/src/ModuleName/Controller/IndexController.php

<?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!',
        ));
    }
}

ModuleName/view/ModuleName/index/index.phtml

<?PHP
$this->headTitle('Some Site...');
echo "$hello";
?>

应用程序/视图/布局/layout.phtml

文档类型();?>
<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>&copy; 2012 - 2013 by YourSite <?php echo $this->translate('All rights reserved.') ?></p>
            </footer>
        </div> <!-- /container -->
        <?php echo $this->inlineScript() ?>
    </body>
</html>
于 2013-02-04T14:16:51.263 回答
-1

无论您在控制器中更改了响应体,当 zf2 MvcEvent::EVENT_RENDER 触发时,都会重建一个新的响应体。所以正确的方法是在MvcEvent::EVENT_RENDER之后更改响应正文。

将此添加到您的控制器:

 $this->getServiceLocator()->get('Application')->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER, function($event){
     $event->getResponse()->setContent('foobar');
 }, -10000);
于 2012-12-21T07:38:48.023 回答
-1

我终于得到了解决方案。要更改布局中的内容,只需输入控制器的操作

$this->layout()->content = 'foobar';
于 2012-12-21T14:08:11.140 回答