zf2扩展现有模块

时间:2014-04-04 14:47:17

标签: php zend-framework2

我一直在使用zf2一段时间,但我无法解决以下问题。

例如。我们有一个名为" Product"它处理大部分产品。 另一个模块 - 让我们称之为"评论" - 应扩展产品模块并扩展产品视图以向用户提供产品评审。

直到这一点似乎没有问题。我们可以轻松覆盖Review模块中的产品视图。

现在我陷入困境的棘手部分。 还有第三个模块 - 让我们来称呼它"社交"。该模块应为整个应用程序提供社交功能。 此模块还应修改产品视图,并在产品页面中添加名为"电子邮件的链接给朋友"。

现在我的问题...... 审阅模块会修改产品视图。如果我覆盖社交模块中的视图,则Review模块中的更改将丢失。

//编辑 信息:社交和评论模块应该能够在不修改产品模块的情况下修改视图。

欢迎任何提示,提示或想法。

1 个答案:

答案 0 :(得分:1)

这取决于您尝试避免重复的代码。

如果只是HTML内容,您可以create a custom ViewHelper呈现所需的HTML。然后可以在需要“社交”内容的每个视图中重复使用它。

我怀疑你是在考虑一个'社交'控制器动作,你希望在其他视图中重用它的返回结果。如果是这样,一个灵魂就是使用forward() controller plugin

来自文档:

  

有时,您可能希望从匹配的控制器中调度其他控制器 - 例如,您可以使用此方法来构建“widgetized”内容。 Forward插件有助于实现[by]返回调度控制器操作的结果

这在像你这样的情况下非常有用,因为你需要“社交”模块作为视图的附加元素;而非替代品。

例如

// SocialModule\Controller\SocialController.php
// Controller action to display social buttons (twitter/facebook etc)
public function socialAction()
{
    // Some controller logic...

    // Returns the social button view
    return new ViewModel(array('foo' => $bar));
}

// ProductModule\Controller\ProductController.php
public function productViewAction()
{
    $product = $this->productService->find($this->params('id'));

    // Displays the product 'view' page
    $view = new ViewModel(array(
        'product' => $product,
    ));

    // Re-disptach the social module action and return it's view model
    $socialView = $this->forward()->dispatch('SocialModule\Controller\Social', array(
        'action'  => 'social',
    ));

    // We now have the view model, attach it to our view
    $view->addChild($socialView, 'social');

    // Return the aggregated view
    return $view;
}

所有需要的是在视图中呈现内容

// product-view.phtml
echo $this->social;    
相关问题