Pyramid:Pyramid / Python 中的 PHP 框架中的 MVC 等效吗?

发布于 2025-01-06 12:21:09 字数 298 浏览 1 评论 0原文

PHP 框架(例如 Kohana)的模型 - 视图 - 控制器的金字塔/Python 等价物是什么?

In Pyramid "Model" is .... and it is used for .....
In Pyramid "View" is .... and it is used for .....
In Pyramid "Controller" is .... and it is used for .....

我试图理解金字塔的逻辑。作为答案的补充,任何帮助、文档等将不胜感激。

谢谢。

What are the Pyramid / Python equivalents of Model - View - Controller of PHP Frameworks such as Kohana?

In Pyramid "Model" is .... and it is used for .....
In Pyramid "View" is .... and it is used for .....
In Pyramid "Controller" is .... and it is used for .....

I am trying to understand Pyramid's logic. As an addition to the answer, any help, documentation etc would be appreciated.

Thanks.

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

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

发布评论

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

评论(4

梦言归人 2025-01-13 12:21:09

Pylons 是连接在一起形成 Pyramid 的两个框架之一(另一个是 repoze.bfg ),它“接近”MVC 系统。

我在引号中加上了引号,因为在过去的几年里,很多人一直在争论 MVC 的含义……并且许多曾经标榜自己为“MVC”的项目开始称其为“MTC”(模型模板控制器)” MT”(模型模板)或“MV”(模型视图)。每个人都同意“模型”是什么,但在给定框架上“视图”和“控制器”到底映射到什么可能是一个争论点。

Pyramid 和 pylon 都具有“调度程序”功能来设置请求的映射。在 pylons 下的 config/routes.py ;在 Pyramid 下有点不同——默认脚手架的路由位于 app/init.py 中,但您可以随意将其分解为 app/routes.py 或使用 config.include()将其推入您的“处理程序”或 config.scan() 以将其从您的“视图”中拉出。

金字塔中的“处理程序”由金字塔处理程序提供,实际上只是带有一堆自动生成内容的“视图”。如果您愿意,您的应用程序可以同时使用处理程序和视图(我的)。

无论如何,根据您如何解释 MVC / MTC / 等,这是您可能想要的松散表:

           || mvt            | mvc            | mvc
==========================================================================
model      || sqlalchemy     | sqlalchemy     | sqlalchemy
view       || views/handlers | templates      | views/handlers + templates
controller ||                | views/handlers | dispatch/routing
template   || templates      |                |

快速说明 - 我定义上述内容不是基于我的解释或“官方”MVC 定义是什么。 ..它是基于其他流行框架如何推销自己的。

Pylons, one of the two frameworks that joined together to be Pyramid ( the other was repoze.bfg ) was "close" to an MVC system.

I put close in quotations, because over the past few years a lot of people have been fighting about what MVC means... and many projects that once touted themselves as "MVC" started to call them "MTC" (model template controller) "MT" (model template) or "MV" (model view). Everyone agrees on what the "model" is, but exactly what the "view" and "controller" map to - on a given framework - can be a point of contention.

Pyramid and pylons both have a "dispatcher" functionality to set up the mapping for a request. Under pylons its in config/routes.py ; under Pyramid it's a little different -- the default scaffolds have the routing in app/init.py , but you're free to break it out into app/routes.py or use config.include() to push it into you 'handlers' or config.scan() to pull it from your 'views'.

'handlers' in pyramid are provided by pyramid_handlers, and are really just 'views' with a bunch of auto-generation stuff in there. If you wanted to, your apps could use both handlers AND views ( mine do ).

In any event, depending on how you interpret MVC / MTC / etc , this is a loose table of what you might want:

           || mvt            | mvc            | mvc
==========================================================================
model      || sqlalchemy     | sqlalchemy     | sqlalchemy
view       || views/handlers | templates      | views/handlers + templates
controller ||                | views/handlers | dispatch/routing
template   || templates      |                |

Quick note- I'm defining the above not based on my interpretation or what the 'official' MVC definition is... It's based in relation to how other popular frameworks market themselves.

孤寂小茶 2025-01-13 12:21:09

如果需要,您可以使用金字塔模拟 MVC 模式:

  • 模型:例如使用 sqlalchemy (http://docs.sqlalchemy.org)
  • 视图:使用模板和视图方法。
  • 控制器:您可以使用包pyramid_handlers来创建控制器并将路由中定义的操作映射到控制器中的操作,例如:
   Class HomeController(object):
     def __init__(self, request):
          self.request = request

      def form_proc(self):
          name = self.request.params['name']
          ... bla, bla, bla ...

在配置中您可以添加类似以下内容:

    config.add_handler('home', '/home/{action}',
                       handler='mypackage.HomeController')

如果您将此网址放入表单操作中 ->; http://SERVER_NAME/home/form_proc,您可以处理表单。

如果您需要,金字塔将为您提供所有灵活性。

If you want, with pyramid you can simulate the MVC pattern:

  • Model: For example using sqlalchemy (http://docs.sqlalchemy.org)
  • View: Using templates and view methods.
  • Controller: You can use the package pyramid_handlers, to create controllers and map actions defined in a route to actions in the controller, for example:
   Class HomeController(object):
     def __init__(self, request):
          self.request = request

      def form_proc(self):
          name = self.request.params['name']
          ... bla, bla, bla ...

In the config you can add something like:

    config.add_handler('home', '/home/{action}',
                       handler='mypackage.HomeController')

If you put this url in your form action -> http://SERVER_NAME/home/form_proc, you can process the form.

Pyramid give you all the flexibility if you need it.

青丝拂面 2025-01-13 12:21:09

来自金字塔简介

你说 Pyramid 是 MVC,但控制器在哪里?

金字塔作者认为 MVC 模式并不真正适合 Web
很好。在金字塔应用程序中,有一个
资源树(表示站点结构)和视图(表示站点结构)
倾向于呈现存储在资源树中的数据和
用户定义的“域模型”。然而,该机构没有提供任何便利
框架实际上必然映射到“控制器”的概念
或“模型”。所以如果你必须给它一些缩写词,我想你会说
Pyramid 实际上是一个“RV”框架而不是“MVC”框架。
然而,“MVC”作为一个通用分类名称已经足够接近了
以便与其他 Web 框架进行比较。

From the Pyramid Introduction:

You Say Pyramid is MVC, But Where’s The Controller?

The Pyramid authors believe that the MVC pattern just doesn’t really fit the web
very well. In a Pyramid application, there is a
resource tree, which represents the site structure, and views, which
tend to present the data stored in the resource tree and a
user-defined “domain model”. However, no facility provided by the
framework actually necessarily maps to the concept of a “controller”
or “model”. So if you had to give it some acronym, I guess you’d say
Pyramid is actually an “RV” framework rather than an “MVC” framework.
“MVC”, however, is close enough as a general classification moniker
for purposes of comparison with other web frameworks.

雨落□心尘 2025-01-13 12:21:09

我有使用 CakePHP 的经验,现在我开始使用 Pyramid 和 Python。
没有直接映射,但这并不是因为金字塔以奇怪的方式做事,而是因为框架作者滥用了 MVC 术语。

例如,在 Cake 中,有一些类喜欢称为“模型”,但大多数时候只是 ORM 类。控制器主要用作称为“操作”的相关方法的命名空间,这些方法将数据传递到视图,而视图只是模板。

用金字塔术语来说,“资源”就是“模型”,你可以随意使用这里的任何地方,如果你想要 ORM,你可以使用 SQLAlchemy 或 mongodb 或任何地方。

框架本身充当“控制器”,操作称为“视图”,这可以是普通函数或类,您可以随意组织它们。该视图可以使用模板和呈现器来构造发送到浏览器的响应。

希望它有帮助(请原谅我的英语不好)

I have experience with CakePHP and now I'm starting with Pyramid and Python.
There is not a direct mapping but it is not because pyramid does things in weird manner but because framework authors abused the MVC term.

In Cake, for example, there are some classes that they like to call 'Models' but most of the time are just ORM classes. The controllers are mostly used as namespaces for related methods called 'actions' that pass the data to the views, which are just the templates.

In pyramid terms, 'Resources' are the 'models', and you are free to use wherever you want here, if you want an ORM you can use SQLAlchemy for example or mongodb or wherever.

The framework itself functions as the 'controllers', and the actions are called 'views' this can be normal functions or classes, you are free to organize them wherever you like. This views may use a template and a renderer to construct the response that is send to the browser.

Hope it helps (please excuse my bad english)

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