如何在 ZF2 中全局将变量传递给 layout.phtml? [英] How to pass variables to layout.phtml globally in ZF2?

查看:22
本文介绍了如何在 ZF2 中全局将变量传递给 layout.phtml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在整个应用程序(全局)中将一系列变量传递给我的 layout.phtml.我的意思是我不想使用

I want to pass a series of variables to my layout.phtml throughout the whole application(globally). And by that I mean I don't wanna use

$this->layout()->someVar = someValue;

在我得到的每一个动作中,因为这将需要大量额外的工作和代码.那么有没有办法在一个地方做到这一点?或者我提到的就是我得到的!希望不是:)

in each and every action I've got, since it would be a lot of extra work and code. So is there a way to do it in just one place? Or What I mentioned is all I got! Hope not :)

也许使用会话?– 雷米托马斯

Maybe using sessions ? – Remi Thomas

感谢您的解决方案.暂时这就是我正在使用的.用于登录用户信息、系统和布局设置以及 ACL 列表.但问题是我必须在 layout.phtml 中定义一个我认为不合适的新对象,是吗?我在某处读到我们需要在视图模型中使用的任何数据都应该使用控制器操作传递给它.特别是我不喜欢偷工减料,所以如果有一种干净的方法可以做到这一点,我宁愿不这样做.最近我必须获取每个用户的未读消息数并在 layout.phtml 中使用它.因此,如果我在 layout.phtml 中执行此操作,它会在视图模型或布局中包含大量 php 脚本.

Thanks for the solution. For the time being that's what I'm using. For logged-in user info, system and layout settings and an ACL list. But the problem is that I have to define a new object in the layout.phtml which I don't think is appropriate, is it? I read somewhere that whatever data we need to use in view models should be passed to it using controller actions. And specially I'm not a fan of cutting corners, so if there's a clean way to do this I'd rather not do it this way. And recently I have to get the number of unread messages for each user and use it in layout.phtml. So if I do it in layout.phtml it's a LOT of php script inside a view model or layout.

谢谢

推荐答案

正如 Orochi 建议的那样,最好和最干净的方法是使用 ViewHelper.这里有一些创建自己的 ViewHelper 的说明:http://framework.zend.com/manual/2.2/en/modules/zend.view.helpers.advanced-usage.html.这不是太复杂;)

The best and cleanest way is using a ViewHelper, as Orochi suggests. Here some instructions to create your own ViewHelper: http://framework.zend.com/manual/2.2/en/modules/zend.view.helpers.advanced-usage.html. It's not too complex ;)

不过,您的问题也可以通过其他方式解决.假设您需要的变量包含由应用程序中已经存在的服务提供的值(然后由 ZF2 ServiceManager 公开),您可以在 Module.php 中的onBoostrap"函数上添加一些行(例如在应用程序模块上).

However your problem could be resolved also in another way. Supposing the variables you need contain values provided by services already present in your application (and then exposed by ZF2 ServiceManager), you could add some lines on the "onBoostrap" function inside your Module.php (e.g. on Application module).

这里有一个例子:

public function onBootstrap($e) {

    $serviceManager = $e->getApplication()->getServiceManager();
    $viewModel = $e->getApplication()->getMvcEvent()->getViewModel();

    $myService = $serviceManager->get('MyModule\Service\MyService');

    $viewModel->someVar = $myService->getSomeValue();

}

通过这种方式,您只能在一处写作业.然后可以像往常一样访问该变量:

In this way you write the assignment in only one place. The variable is then accessible as usual:

$this->layout()->someVar;

这篇关于如何在 ZF2 中全局将变量传递给 layout.phtml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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