PHP MVC 问题
给定一个 Controller
类和一个 View
类,控制器直接为视图属性赋值更好,还是在控制器中为属性赋值更好?然后在准备显示时将这些属性复制到视图中?
示例模型类
class Model
{
public $propertyA;
public $propertyB;
}
示例控制器类:
class Controller
{
protected $view;
protected $model;
public function __construct()
{
$this->model = new Model();
$this->view = new View();
$this->prepareData();
$this->initView();
}
protected function prepareData()
{
$this->model->propertyA = 'This is property A.';
$this->model->propertyB = 'This is property B.';
}
protected function initView()
{
$this->view->model = $this->model;
$this->view->display();
}
}
示例视图类:
class View
{
public $model;
public function display()
{
echo "propertyA = $this->model->propertyA";
echo "propertyB = $this->model->propertyB";
}
}
抱歉,我累了。我确实使用模型,因此请考虑到这一点重新考虑您的答案。
Given a Controller
class and a View
class, is it better for the controller to directly assign values to view properties or, is it better to assign values to properties in the controller and then copy those properties to the view when ready to display it?
Example Model Class
class Model
{
public $propertyA;
public $propertyB;
}
Example Controller class:
class Controller
{
protected $view;
protected $model;
public function __construct()
{
$this->model = new Model();
$this->view = new View();
$this->prepareData();
$this->initView();
}
protected function prepareData()
{
$this->model->propertyA = 'This is property A.';
$this->model->propertyB = 'This is property B.';
}
protected function initView()
{
$this->view->model = $this->model;
$this->view->display();
}
}
Example View class:
class View
{
public $model;
public function display()
{
echo "propertyA = $this->model->propertyA";
echo "propertyB = $this->model->propertyB";
}
}
Sorry, I was tired. I do use a model, so please reconsider your answers with this in mind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据应该只在一处。如果不是这样,当事情变得复杂时,就很难同步拥有数据的不同位置。在 MVC 中,您有一个模型,这就是数据所在的位置。将模型传递到视图中并让视图显示该模型。
这是一个简单的解释:http://en. wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller 或者对于那些不喜欢维基百科的人:http://ootips.org/mvc-pattern.html
该模型可以像一个包含属性的类一样简单。
The data should only be in one place. If not when things get complicated it is hard to sync the different places you have the data. In MVC you have a model and that is where the data should be. Pass the Model into the View and have the view display that.
Here is a simple explanation: http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller or here for those that do not like Wikipedia: http://ootips.org/mvc-pattern.html
The model can be as simple as a class with the properties in it.
视图不应该设置变量,除非它们与演示文稿相关。无论如何,最好将静态变量放入配置文件中。
而不是在视图中设置变量,为什么不直接使用对控制器的引用来构造视图。这应该可以让您免于编写大量样板代码。
编辑: 我比我自己更喜欢 Romain Hippeau 的答案。您应该将模型传递到视图中。
The view shouldn't be setting up variables unless they are related to the presentation. It's best to put static variables in a config file anyway.
Rather than setting variables in the view why don't you just construct the view with a reference to the controller. That should save you from writing a lot of boiler plate code.
Edit: I like Romain Hippeau's answer a lot more than my own. You should pass the model into the view.