2

可能重复:
如何在 ZF2 中使用 ZF1 $this->action?

我从几年前就开始使用 PHP,我非常了解 OOP,但我是 Zend Framework 2 的新手,我在它的概念上遇到了一些麻烦。

我正在尝试开发一个网站,已经完成,但现在使用 ZF2。

我最大的问题之一是布局/视图(我已经知道它们之间的区别)。

在该网站中,有一个动态生成的侧边栏菜单(从 DB 中提取的内容......让我们在 CategoriesController 中调用 sideBarGenAction)。而且我需要在每个页面(布局模板内)中显示它。所以,这就是我的疑问:如何做到这一点,而不在每个其他动作中调用那个动作(sideBarGenAction)?

如果侧边栏有静态内容,我可以使用 $this->partial,但它有动态内容,并且需要传递部分变量。

我想我可以创建一个 ViewHelper,但它没有多大意义(因为它不是一个小部件,它是一个模型内的业务规则......如果我必须为我需要的所有内容创建一个 ViewHelper称为“视图内”,我不需要 MVC 框架)。

4

1 回答 1

3

因此,您的模型应该具有可重用的单元可测试方法来获取数据片段,可以是字符串、对象、数组等。这是正确处理数据的逻辑,因此它可以以有意义的方式使用,而且可以是单元测试。

在控制器中,您实例化该模型的一个实例,并使用您需要的方法来获取您想要使用的数据。然后你将它传递给视图。现在这可以在操作本身中,或者,您可以创建一个可重用的方法来执行此操作并返回数据,以便其他操作也可以使用它。如果这也可以在其他控制器中使用,那么您可能应该创建一个控制器使用的动作助手......你明白了......有很多方法可以构建相同的东西,具体取决于您对可重用代码的关注程度。

要将数据从控制器传递到 zf1 中的视图,您需要这样做

$this->view->whateverItIsCalled = $someValueOrArrayTypeThing;

然后在视图中使用它,回显它等等,比如$this->whateverItIsCalled.

在zf2中是这样的

namespace Content\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class ArticleController extends AbstractActionController
{
    public function viewAction()
    {
        // get the article from the persistence layer, etc...

        $view = new ViewModel();

        $articleView = new ViewModel(array('article' => $article));
        $articleView->setTemplate('content/article');

        $primarySidebarView = new ViewModel();
        $primarySidebarView->setTemplate('content/main-sidebar');

        $secondarySidebarView = new ViewModel();
        $secondarySidebarView->setTemplate('content/secondary-sidebar');

        $sidebarBlockView = new ViewModel();
        $sidebarBlockView->setTemplate('content/block');

        $secondarySidebarView->addChild($sidebarBlockView, 'block');

        $view->addChild($articleView, 'article')
             ->addChild($primarySidebarView, 'sidebar_primary')
             ->addChild($secondarySidebarView, 'sidebar_secondary');

        return $view;
    }
}

并且视图将像您之前在 zf1 中使用的东西一样使用它,例如

<?php // "content/article" template ?>
<div class="row content">
    <?php echo $this->article ?>

    <?php echo $this->sidebar_primary ?>

    <?php echo $this->sidebar_secondary ?>
</div>

<?php // "content/article" template ?>
    <!-- This is from the $articleView View Model, and the "content/article"
         template -->
    <article class="span8">
        <?php echo $this->escapeHtml('article') ?>
    </article>

<?php // "content/main-sidebar" template ?>
    <!-- This is from the $primarySidebarView View Model, and the
         "content/main-sidebar" template -->
    <div class="span2 sidebar">
        sidebar content...
    </div>

<?php // "content/secondary-sidebar template ?>
    <!-- This is from the $secondarySidebarView View Model, and the
         "content/secondary-sidebar" template -->
    <div class="span2 sidebar pull-right">
        <?php echo $this->block ?>
    </div>

<?php // "content/block template ?>
        <!-- This is from the $sidebarBlockView View Model, and the
            "content/block" template -->
        <div class="block">
            block content...
        </div>

您可以看到您如何实例化一个新的视图模型,然后不仅将内容传递给视图,还可以指定更具体的部分,这是不同的。

无论如何,希望对你有所帮助。基本上,长话短说,ZF2 比 ZF1 提高了性能有几个原因,但减少开销是一个重要的原因。很多框架,事实上,大多数都是这样做的,因为你只在需要时才需要它。

于 2012-11-28T02:57:02.393 回答