使用 MVC 制作 PHP 应用程序。如何使用多个模型创建页面?
我正在尝试学习如何使用 MVC 架构制作 PHP 应用程序。我根据教程创建了一个基本应用程序,该应用程序只有一个模型、一个视图和每个页面组的控制器(例如 site.com/index、site.com/account 等)。
我现在的问题是,我不知道如何使事情变得更复杂。假设我想制作一个日历应用程序,用户在登录时会在其帐户页面上看到一个日历。
我会为日历创建一个模型和视图,然后通过将日历模型包含到其中来获取所需的日历数据吗?帐户页面控制器?
在渲染日历时,我是否可以将日历视图包含到帐户页面视图中,然后使用它来生成日历布局?
I'm trying to learn how to make a PHP app using an MVC architecture. I've created a basic app based on a tutorial which just has a model, a view and a controller for each page group (such as site.com/index, site.com/account and so on).
My problem now, is that I can't figure out how I would make something more complex. Say, I want to make a calendar app, in which the user sees a calendar on their account page when they log in.
Would I create a model and view for the calendar, and then get the required calendar data by including the calendar model into the account page controller?
How about when rendering the calendar, would I include the calendar view into the account page view and then use that to generate my calendar layout?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说你有一个很好的主意。将日历视图视为“部分视图”,基本上它不是完整的 html 页面,只是 div 中的日历。您可以将此部分视图包含在帐户视图中,并根据需要传递数据。是的,您可以使用日历模型在帐户控制器中获取日历数据,然后可以将该数据发送到帐户视图并进一步传递到部分日历视图。
You have a pretty good idea i would say. Think of the calendar view as a "partial view" basically it's not a full html page, just the calendar in a div. You would include this partial view within the account view and pass in the data as needed. Yes, you can grab the calendar data in the account controller using the calendar model, this data can then be sent to the account view and further passed on to the partial calendar view.
这实际上取决于您如何设置应用程序。您也许可以
include()
正确的文件,然后调用它们的方法或函数来处理您的数据。您的应用程序是否使用了任何类型的命名约定?如果是这样,您可能需要命名控制器并对特定的内容进行建模,并定义模型内部数据之间的关系。不管怎样,一些代码示例可能会帮助我们提供更明确的答案。
It really depends on how you setup your app. You may be able to just
include()
the proper files, and then call their methods or functions to work w/ your data. Did you app use any sort of naming convention? If so you may need to name your controllers and model something specific and define relationships between the data inside of the models.Either way some code samples would probably help us provide a more definite answer.