关于构建自己的(PHP)MVC框架的问题

发布于 2024-09-24 06:05:54 字数 259 浏览 9 评论 0原文

这时候我想搭建一个MVC框架。一切都很顺利,但现在我在控制器和视图之间遇到了问题。希望你能帮助我。

我的 MVC 与 MVC 没有太大不同,它使用相同的概念,但从技术上讲我遇到了问题。问题是:加载视图后如何从控制器获取数据?

请记住,在视图中,您不使用全局变量或类似的东西,每个参数(如数据库回复等)都必须传递给视图,并且视图直接使用它(如......例如,CodeIgniter)。

我不知道该怎么说比较好,如果你不明白我的意思,我很抱歉。但希望你可以!

At this time I want to build a MVC framework. Everything is going fine but now I got a problem between controller and view. Hope you can help me.

My MVC is not so different from MVC, it uses the same concept, but technically I got an issue. The question is: how to take data from the controller once the view is loaded?

Bear in mind that in views you don't use globals or something like, every parameter (like DB reply, etc) must be passed to the view and the view uses it directly (like... for example, CodeIgniter).

I don't know how to say it better, I'm sorry if you don't understand me. But hope you can!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧伤慢歌 2024-10-01 06:05:54

您可能希望将变量列表绑定到视图的命名空间。

您可以仔细阅读 Kohana 的 View 类 以获得一些灵感或想法。

You probably want to bind a list of variables to the view's namespace.

You could peruse Kohana's View class for some inspiration or ideas.

一曲琵琶半遮面シ 2024-10-01 06:05:54

MVC 框架背后的要点是视图不需要了解控制器及其实现。视图应该只具有正确呈现所需的变量列表,并且控制器实现担心确保这些变量存在于视图中。

请问您为什么要设计自己的 MVC 框架?那里有很多很棒的解决方案,我个人建议您尝试 CakePHP,我已经用它来发布了几个商业产品,我发誓!

The point behind the MVC framework is that a View should not need to be aware of the Controller and it's implementation. A View should just have a list of variables that it needs to be present to render correctly, and the Controller implementation worries about making sure those variables are present in the View.

Might I ask why you are designing your own MVC framework though? There are plenty of fantastic solutions out there, I would personally recommend you try out CakePHP, I've used it to ship several commercial products and I swear by it!

帅气称霸 2024-10-01 06:05:54

您必须向视图传递对控制器对象的引用,或者视图必须在相关控制器类上使用静态方法。然后视图调用控制器上的方法来显示结果。视图还通过控制器对象或类向控制器提交表单数据。

You must pass the view a reference to the controller object or the view must use static methods on a related controller class. The view then calls methods on the controller to display results. The view also submits form data to the controller through the controller object or class.

旧夏天 2024-10-01 06:05:54

如果您的 View 实现使用模板引擎,通常有一种机制可以将任意键/值数据添加到模板的范围。如果您还没有达到这一点,那么可能值得考虑类似的策略。

If your View implementation uses a template engine, there is usually a mechanism for adding arbitrary key/value data to the template's scope. If you haven't progressed to that point it's probably worth thinking about a similar strategy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文