Php,如果我有一个菜单类,如何显示它? (MVC逻辑)
我有一个菜单类:
addMenuItem ($parent)
modifyLinkTarget ($item)
等等。现在我将使用 show()
方法显示它,该方法应该生成 HTML 输出。但这不是一个好方法,因为如果我有多个模板,而它生成一个 HTML,该怎么办? CSS 不会帮助独立:)那么如何显示它而不损害 MVC 和灵活性呢?
I have got a menu class:
addMenuItem ($parent)
modifyLinkTarget ($item)
etc. Now I will show it with a show()
method, which should produces HTML output. But its not a good way, because what if I have several templates while it produces one HTML. CSS wont help standalone :) so how to display it not to hurt MVC and being flexible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
菜单类包含您的项目、链接等,并且是您的模型。正如您所说,模型不输出任何内容,这是视图的任务。所以你必须将模型(菜单类对象)传递到你的视图,在那里你可以输出它。为此,您的菜单类可能需要一些额外的方法,例如 getAllMenuItems(int $parentItem) 或类似的方法。在您看来,您可以执行以下操作:
如您所见,您可能必须使用 menuItem 类扩展菜单模型,以遵循 OOP 方式。您的菜单类组织了多个 menuItem 对象。
总的来说,你必须有以下情况:
The menu class contains your items, links etc. and is your model. As you correctly said, the model does not output anything, it is the task of the view. So you have to pass the model (menu class object) to your view, where you can output it. For this, your menu class might need some additional methods like getAllMenuItems(int $parentItem) or something like that. In your view, you can do something like this:
As you can see, you might have to extend your menu model with a menuItem class, to follow the OOP way. Your menu class organizes several menuItem objects.
Overall, you have to following situation: