MVC模式(需要在模型中查看对象)
我在我的应用程序中使用 MVC 模式。
现在我需要模型中的视图对象。
我不想将视图添加为模型中函数的参数(因为我在其他函数中也需要它)。我不想继续这样下去。
是否应该将视图添加为模型构造函数的属性?
还有别的办法吗?我不应该首先需要模型中的视图对象吗?
首选的做法是什么?
示例:
控制器
function someAction()
{
$somemodel->add();
}
模型
class SomeModel()
{
function add()
{
if ($view->user) {
// do stuff
$this->mail();
} else {
// do other stuff
}
}
function mail()
{
Mailer::send($view->user->email, $this->getitems(), $view->layout);
}
function getitems()
{
return Items::getitems($view->user);
}
}
I'm using the MVC pattern in my application.
Now I need the view object in a model.
I don't want to add the view as a parameter for my function in the model (since I need it in other functions as well). And I don't want to keep on passing it.
Should a add the view as an attribute for the constructor of the model?
Is there another way? Shouldn't I be needing the view object in the model in the first place?
What would be the preferred way of doing it?
Example:
Controller
function someAction()
{
$somemodel->add();
}
Model
class SomeModel()
{
function add()
{
if ($view->user) {
// do stuff
$this->mail();
} else {
// do other stuff
}
}
function mail()
{
Mailer::send($view->user->email, $this->getitems(), $view->layout);
}
function getitems()
{
return Items::getitems($view->user);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实在使用 MVC,那么您将不需要模型中的视图,因为只有控制器应该有权访问该视图。
查看您提供的代码,我可以告诉您一件事:
add()
方法不应以任何方式引用$view
(即使是访问其属性)。相反,模型应该由控制器提供$view->user
值。mail()
方法也是如此。考虑解决这些问题。否则,以后你会陷入更糟糕的境地。
If you're really doing MVC, then you won't need the view in the model, because only the controller should have access to the view.
Looking at the code you've provided, I can tell one thing: the
add()
method should not reference$view
in any way (even for accessing its properties). Instead, the model should be provided with the$view->user
value from the controller. The same goes for themail()
method.Consider fixing those issues. Otherwise, you'll get into something worse later on.
模型应该与视图分开。因此,正如 mkArtak 所说,控制器应该是唯一与视图通信的东西。然后仅将必要的信息传递给模型。
至于模型,它实际上应该只处理它理解的信息。
也就是说,如果您有一个汽车模型...您不想依赖它的工厂来构建它。如果这样做,如果您想在不同的工厂中构建它,则必须更改代码。
The model should be separate from the view. So, as mkArtak said, the controller should be the only thing that communicates with the view. Which then passes only the necessary information to the model.
As for the model, it should really only deal with the information that it understands.
i.e. if you had a Car model... you don't want to build it dependent on it's factory. If you did, you would have to change your code if you wanted to build it in different factory.
控制器是您“烘焙”渲染准备工作的地方。我所说的烘焙是指您考虑传入的任何 $_REQUEST 参数,进行模型 API 调用以获取所需的数据,并设置要呈现的模板变量。在此过程结束时,您的操作应该调用您选择的模板(视图),以便呈现“烘焙”模板变量。
The controller is where you 'bake' everything prepare for render. By bake I mean you consider any passed in $_REQUEST params, make model API calls to get the data you need, and set template variables to be rendered. Your action, at the end of this process should make a call to a template (view) you choose in order to render the 'baked' template variables.