不依赖框架的MVC教程?

发布于 2024-07-06 13:33:24 字数 1560 浏览 11 评论 0原文

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

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

发布评论

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

评论(5

失眠症患者 2024-07-13 13:33:24

几乎每个框架的 MVC 方式都不同,因此您最终可能会更加困惑。 MVC 的一般原则非常简单:“模型是状态;视图对模型做出反应;控制器对视图作出反应;控制器更改模型”。 模型、视图和控制器都是概念——它们就是你感觉的任何东西。 类、类群、带有 XML 配置文件的类实例,应有尽有。

我实际上认为这涵盖了基本原则。 如果没有框架,你就不会走得更远。 重要的是特定框架如何定义模型、视图和控制器以及它们的交互。

Almost every framework does MVC differently, so you might end up getting even more confused. The general principles of MVC are very simple: "Model is state; view reacts to model; controller reacts to view; controller changes model". The model, view and controller are concepts - they are whatever you feel them to be. Classes, bunches of classes, instances of classes with XML configuration files, you name it.

I actually think that about covers the basic principles. Without a framework, you'd not get much further. What matters is how a particular framework defines model, view and controller and their interactions.

寒冷纷飞旳雪 2024-07-13 13:33:24

MVC 基本上只是将代码拆分为处理数据的模型、显示数据的视图以及将数据从模型传递到视图的控制器。

您不需要 API 或框架,它只是一种拆分代码的方法。 许多框架使用它的原因是因为它是一个非常简单的概念,它适用于很多事情(它完美地适合网页),并且相当灵活(例如,使用 Rails,您可以做所有事情)你的视图,或模型/控制器,如果你需要的话..)

Python 中的一个快速示例,示例 MVC 结构化 Python 脚本。 不一定是“最佳实践”,但它有效,而且相当简单:

class Model:
    def get_post(self, id):
        # Would query database, perhaps
        return {"title": "A test", "body": "An example.."}

class Controller:
    def __init__(self):
        self.model = Model()
        self.view = View()

    def main(self):
        post = self.model.get_post(1)
        self.view.display(post)

class View:
    def display(self, item):
        print "<h1>%(title)s</h1>\n%(body)s" % item

c = Controller()
c.main()

MVC is basically just splitting up your code into a Model, which deals with the data, a View which displays the data, and a Controller which passes data from the Model to the View.

It's nothing you need an API or framework for, it's just a way of splitting up your code. The reason many frameworks use it is because it's a very simple concept, it works well for many things (it fits webpages perfectly), and is fairly flexible (for example, with Rails, you could do everything in your view, or model/controller, if you so-desired..)

A quick example in python, of an example MVC structured Python script. Not necessarily "best practices", but it works, and is fairly simple:

class Model:
    def get_post(self, id):
        # Would query database, perhaps
        return {"title": "A test", "body": "An example.."}

class Controller:
    def __init__(self):
        self.model = Model()
        self.view = View()

    def main(self):
        post = self.model.get_post(1)
        self.view.display(post)

class View:
    def display(self, item):
        print "<h1>%(title)s</h1>\n%(body)s" % item

c = Controller()
c.main()
在梵高的星空下 2024-07-13 13:33:24

除了 Sander 的回复之外,我想说大多数框架都混淆了前端控制器MVC。 它们实际上是两个完全独立的概念,但它们通常都存在于框架中。 所以要小心。

In addition to Sander's reply, I'd say that most frameworks confuse front controller and MVC. They are really two completely separate concepts, but they are often both present in frameworks. So watch out for that.

愁杀 2024-07-13 13:33:24

MVC 的主要优点是关注点分离。 当你编写代码时,如果你不小心,它可能会变得一团糟。 因此,从长远来看,了解如何将模型、视图和控制器放在不同的“孤岛”中可以节省您的时间。 任何策略都是好的。

所以这是我的:

  • 模型是项目树中 /lib 下找到的文件,
  • 视图是项目树中以 .html 结尾的文件,
  • 控制器是
    中的 url。 动作属性

The main advantage of MVC is separation of concerns. When you write code, and if you're not careful, it can become a big mess. So knowing how to put Models, Views, and Controllers in different "silos" saves you time in the long term. Any strategy is good.

So here is mine :

  • models are files found under /lib in the project tree
  • views are files ending in .html in the project tree
  • controllers are urls in <form> action attributes
谷夏 2024-07-13 13:33:24

我知道现在已经晚了,但我相信稍后人们会提出同样的问题。

我认为上面非常好的代码示例最好像这样放置,但是 YMMV:

#!/usr/bin/python
class Model:
  def get_post(self):
    return {"title":"A test","body":"An example.."}

class View:
  def display(self,items):
    print 'Title:',items['title'],'\n'+'Body:',items['body']

class Controller:
  def __init__(self):
    self.model=Model()
    self.view=View()

  def main(self):
    post=self.model.get_post()
    self.view.display(post)

mvc=Controller()
mvc.main()

这是另一个使用继承的示例,它在 python/php 中非常有用......

#!/usr/bin/python3
class Control:
  def find(self,user):
    return self._look(user)

  def _look(self,user):
    if user in self.users:
      return self.users[user]
    else:
      return 'The data class ({}) has no {}'.format(self.userName(),user)

  def userName(self):
    return self.__class__.__name__.lower()

class Model(Control):
  users=dict(one='Bob',two='Michael',three='Dave')

class View():
  def user(self,users):
    print(users.find('two'))

def main():
  users=Model()
  find=View()
  print('--> The user two\'s "real name" is:\n')
  find.user(users)

if __name__=="__main__":
  main()

如果这有意义,请转到 django,现在您准备好了。 只要阅读免费书籍(如果这有意义),您就会快速浏览它。 你的权利,但在使用 django 之前你必须能够了解 OOP 和 MVC 范例,因为它是通过这些范例构建和使用的。

正如您所看到的,它并不复杂,它只是保持代码有序的众多方法之一。

这解释了 django 中的 MVC

Know it is late but I am sure people will come along later with the same question.

I think the very good code example above is better put like this but YMMV:

#!/usr/bin/python
class Model:
  def get_post(self):
    return {"title":"A test","body":"An example.."}

class View:
  def display(self,items):
    print 'Title:',items['title'],'\n'+'Body:',items['body']

class Controller:
  def __init__(self):
    self.model=Model()
    self.view=View()

  def main(self):
    post=self.model.get_post()
    self.view.display(post)

mvc=Controller()
mvc.main()

Here is another example using inheritance which can be very useful in python/php.....

#!/usr/bin/python3
class Control:
  def find(self,user):
    return self._look(user)

  def _look(self,user):
    if user in self.users:
      return self.users[user]
    else:
      return 'The data class ({}) has no {}'.format(self.userName(),user)

  def userName(self):
    return self.__class__.__name__.lower()

class Model(Control):
  users=dict(one='Bob',two='Michael',three='Dave')

class View():
  def user(self,users):
    print(users.find('two'))

def main():
  users=Model()
  find=View()
  print('--> The user two\'s "real name" is:\n')
  find.user(users)

if __name__=="__main__":
  main()

If this makes sense go to django now your ready. Just read the free book if this makes sense you will go through it rapidly. Your right though you must be able to know about OOP and MVC paradigms before using django as it is built and used via these paradigms.

As you see it is not complex it is just one of the many ways to keep your code in order.

This explains MVC in django

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