从控制器到视图
我正在致力于创建自己的非常简单的 MVC,并且正在集思广益如何从控制器转到视图。其中涉及将变量从类发送到普通的旧 PHP 页面。
我确信以前已经讨论过这个问题,但我想看看人们能想出什么样的想法。
//this file would be /controller/my_controller.php
class My_Controller{
function someFunction(){
$var = 'Hello World';
//how do we get var to our view file in the document root?
//cool_view.php
}
}
I am working on creating my own very simple MVC and I am brainstorming ways to go from the controller to the view. Which involves sending variables from a class to just a plain old PHP page.
I am sure that this has been covered before, but I wanted to see what kind of ideas people could come up with.
//this file would be /controller/my_controller.php
class My_Controller{
function someFunction(){
$var = 'Hello World';
//how do we get var to our view file in the document root?
//cool_view.php
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
某种哈希表是做到这一点的好方法。将变量作为关联数组返回,这将填补视图中的所有空白。
Some kind of hashtable is a good way to do that. Return your variables as association array which will fill all the gaps in your view.
将变量存储为控制器对象中的属性,然后在渲染时提取它们
您可以定义那些神奇的 __get 和 __set 方法以使其更漂亮
Store your variables as a property in your controller object, then extract them when rendering
You can define those magic __get and __set methods to make it prettier
我也在开发自己的简单 MVC,
最简单的方法
来做到这一点是...查看类
template_filename.php
我强烈建议您看看 PHP Savant http://phpsavant.com/docs/
I'm also developing my own simple MVC and the
most simple way
to do it is ...View class
template_filename.php
I highly recommend you to take a look at PHP Savant http://phpsavant.com/docs/
我会查看
Zend_View
以及如何它完成了视图渲染。您可以获取
的源代码查看
和AbstractView
- 不幸的是,我找不到易于浏览的当前存储库(在 svn 中)。本质上,视图变量包含在
View
对象(您的控制器可以访问该对象),然后模板(普通的旧 php 文档)将在该对象内呈现。该方法允许模板访问$this
。它会是这样的:
所以在你的控制器中:
和你的模板:
在我看来,这种模式可以让你在视图方面有很大的灵活性。
I'd checkout
Zend_View
and how it accomplished view rendering.You can get the source of
View
andAbstractView
on github - unfortunaly I don't find the current repository (in svn) that easy to browse.Essentially the view variables are contained in a
View
object (which your controller would have access to), then the template (plain old php document) is rendered inside that object. That method allows the template access to$this
.It would be something like:
So in your controller:
And your template:
In my opinion this pattern allows you much flexibility with the view.
我为一些想要提高 PHP 水平的人开设的免费 PHP 课程创建了自己的 MVC。
到目前为止,最好的方法是使用 Command + Factory 模式。
例如
,在每个控制器中:
从主前端控制器:
要点是每个 Controllercommand 类都有一个执行函数,并返回其视图和该视图的任何数据。
对于完整的 MVC,您可以通过发送电子邮件至 theodore[at]phpexperts.pro 来访问开源应用程序。
I created my own MVC for the free PHP course I'm conducting for a handful of people wanting to get better at PHP.
By far the best way to do this is to use the Command + Factory pattern.
E.g.
In each controller:
From your main front controller:
The point is that every Controllercommand class has an execute function and that returns its view and any data for that view.
For the complete MVC, you can access the open source app by emailing me at theodore[at]phpexperts.pro.