zend 2:自动为模块的所有操作注入模板,而无需修改站点范围的配置 [英] zend 2: Automatically inject a template for all actions of a module without modifying site wide configuration

查看:89
本文介绍了zend 2:自动为模块的所有操作注入模板,而无需修改站点范围的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Zend骨架应用程序.

I am using the Zend skeleton application.

我想在默认的网站范围菜单栏下方插入一个子菜单栏(通过模板),但是我不想修改网站范围的应用程序设置,我希望将其放在模块中

I want to have a sub menu bar (via a template) injected underneath the default site wide menu bar, but I don't want to modify the site wide app settings, I'd like to just have it in the module.

查看示例,似乎我必须手动将菜单栏模板插入控制器的每个操作以及希望其显示如下的每个模板中:

Looking at examples it seems I would have to manually inject the menu bar template in each of my controller's actions and in every template where I want it to appear like this:

public function indexAction() {
    $view = new ViewModel();

    $subNavView = new ViewModel();
    $subNavView->setTemplate('helpdesk/helpdesk/subNav');
    $view->addChild($subNavView, 'subNav');

    return $view;
}

public function someAction() {
    $view = new ViewModel();

    $subNavView = new ViewModel();
    $subNavView->setTemplate('helpdesk/helpdesk/subNav');
    $view->addChild($subNavView, 'subNav');

    ....do something add variables to $view....

    return $view;
}

public function someOtherAction() {
    $view = new ViewModel();

    $subNavView = new ViewModel();
    $subNavView->setTemplate('helpdesk/helpdesk/subNav');
    $view->addChild($subNavView, 'subNav');

    ....do something add variables to $view....

    return $view;
}

...etc

每个模板中都有"echo $ this-> subNav".

And the "echo $this->subNav" in every template.

这是正确的方法还是有办法让我的模块自动为每个页面包括此模板(无需在单个模块之外进行任何修改)?

Is this the right way to do this or is there a way to have my module automatically include this template for every page (without modifying anything outside of the individual module)?

我阅读了文档 ,但我仍然对如何实现这一目标,甚至可能实现这一目标感到困惑.

I read the docs, but I'm still confused on how to achieve this or if this is even possible.

推荐答案

如果您想直接更新视图模型,也可以在Module类的onBootstratp中进行:

If you want to update the view model directly, you could also do that in onBootstratp of your Module class:

public function onBootstrap($e) {
    $view = $e->getApplication()->getMvcEvent()->getViewModel();
    $subNavView = new ViewModel();
    $subNavView->setTemplate('helpdesk/helpdesk/subNav');
    $view->addChild($subNavView, 'subNav');
}

当然,您的布局中已经有了类似的内容:

And of course you already have something like this in your layout:

<?php echo $this->subNav; ?>

您还可以考虑使用标准的局部视图帮助器,并从Module.php中将模板路径设置为模型中的变量,如下所示:

You could also consider using the standard partial view helper and setting the template path as a variable in your model from your Module.php like this:

public function onBootstrap($e) {
    $viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
    $viewModel->subNav = 'application/navigation/subNav.phtml';
}

然后,您可以修改/module/Application/view/layout/layout.phtml这样的内容

Then you modify your /module/Application/view/layout/layout.phtml something like this

<?php if ($this->subNav) {
    echo $this->partial($this->subNav);
} ?>

该想法的缺点是,您将拥有一个视图模型变量,该变量将显示在所有模型中.例如,在json结果中,这可能很烦人.

The drawback to this idea is that then you have a view model variable which will show up in all your models. This can be annoying, for example, in json results.

最后一个想法,您可能需要考虑使用导航视图帮助器.如果不这样做,您可以实现 https://github.com/spiffyjr/spiffy-navigation 想要从头开始构建一个.

Last idea, you might want to consider a navigation view helper. You could implement https://github.com/spiffyjr/spiffy-navigation if you don't want to build one from scratch.

如果使用视图助手(自定义或预先存在的软件包,例如Spiffy Jr's)解决问题,则需要修改布局,使其使用诸如此类的助手,并且所有逻辑均由助手类提供:

If you solve it with a view helper, either custom or pre-existing package such as Spiffy Jr's, then you'd modify your layout so it uses the helper something like this, and all the logic is provided by the helper class:

<?php echo $this->navigationMenu(); ?>

这三个想法将使您的控制器变得整洁,并让您的模块以与其相关的方式来设置subNav,例如它适用于哪些路由等.

All three ideas will unclutter your controllers and let your Module set up the subNav in a way that is relevant to it, such as which routes it is valid for, etc.

这篇关于zend 2:自动为模块的所有操作注入模板,而无需修改站点范围的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆