MVC PHP:嵌套视图时如何最大限度地减少冗余数据?
我有自己的 MVC 框架,与 CodeIgniter 非常相似。
任何视图都会加载为 loadView($view_name, $parameters);
假设有 3 个视图:A、B 和 B。 C.
- A 加载 5 个 B,B 依次加载 5 个另一个 C
- 每个 C 有一个表单,我们需要在其中显示登录的用户名和登录的个人资料图片
- 因此,我们传递 5*5 = 25 次相同的用户名和个人资料图片登录用户
这很糟糕!我们不能在视图中使用 SESSION 或其他 GLOBAL 变量。减少这种冗余的最佳解决方案是什么?
问候, 桑克特
I have my own MVC framework, very similar to CodeIgniter.
Any view is loaded as loadView($view_name, $paramaters);
Lets say there are 3 Views, A,B & C.
- A loads 5 B's and B in turn loads 5 another C's
- Each C has a form where we need to display logged in username and logged in profile pic
- So we're passing 5*5 = 25 times the same username and profile pic of logged in user
That's bad! We cannot use SESSION or another GLOBAL variable inside a view. What would be the best solution for this to reduce this redundancy?
Regards,
Sanket
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不会创造新的变量吧?你只是传递现有的变量。或者无论如何你都应该这样。这本身并没有什么错。真的。
You're not making new variables are you? you're just passing existing variables around. Or you should be anyway. Nothing inherently wrong with that. Really.
内存方面应该不重要,但是多次设置数据会使您的代码变得混乱。我之前所做的是,不是让嵌套视图保存自己的数据,而是让父视图渲染子视图,以便所有视图都可以访问父视图中保存的数据。在我的观点中,我允许要么/要么。您可以添加可以保存自己的数据并自行呈现的子视图,也可以添加由父视图呈现并可以访问父数据的子模板。
it shouldn't matter memory wise, but setting the data that many times can clutter your code. what I have done before is, instead of having the nested views hold their own data, have your parent view render the child views so that all views can access the data held in the parent. in my view class i allow for either/or. you can add child views which can hold their own data and render themselves, or you can add child templates, which are rendered by the parent view and can access the parent data.