PHP:Zend_Layout:在哪里编写业务逻辑?
在大多数项目中,我使用多个布局脚本。
有时我需要布局中的一些特定于布局的数据,而不是特定于页面的数据。但使用哪种布局是特定于页面的。
因此引导程序或动作控制器是选择布局的好地方。但恕我直言,它们不是是注入特定布局所需数据的好地方。
我能想到的唯一其他地方就是在布局视图脚本本身中编写一些业务逻辑。虽然我也不想这样做:)
你在哪里编写特定于布局的业务逻辑?
-- 更新:
特定于布局的业务逻辑可以是;
- 当前登录用户的用户名
- 用户收件箱中的新消息数量
- 随机“你知道吗..?”提示
On most project I use multiple layout scripts.
Sometimes I need some data in my layouts that are layout-specific, instead of page-specific. Which layout gets used though, IS page-specific.
So the bootstrap or actioncontroller would be good places to select a layout. But IMHO they would not be good places to inject the data a particular layout expects.
The only other place I can think of is to just write some business logic in the layout viewscript itself. Though that's something I'd rather not do either :)
Where do you write your layout-specific business logic?
-- UPDATE:
layout-specific business logic could be;
- username of currently logged-in user
- amount of new messages in user's inbox
- random "did you know..?" tip
像这样的事情最好从 ViewHelper 完成
然后在布局中将其与
一起使用,请注意,这是示例代码,不打算在任何地方运行。具体如何从 Helper 访问模型以及如何返回内容完全取决于您。您还必须找到一种方法来向布局注册 ViewHelpers。并且会有人告诉您,您可能无法从视图访问模型(这是错误的)
另请参阅这些相关问题:
看看如何Zend Framework 为
Stuff like this is best done from a ViewHelper
Then in your layout, use it with
Note that this is example code not intended to run anywhere. Exactly how you access your model from the Helper and how you return the content is completely up to you. You will also have to find a mean to register the ViewHelpers with the Layout. And there will be people telling you, you may not access the model from the View (which is wrong)
Please also see these related questions:
And have a look at how Zend Framework does this kind of work for further information.