Drupal 的引导/调度/路由流程
假设安装了最小模块(为了简单起见),Drupal 的 index.php
中两个顶级函数的核心“职责”是什么?
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
menu_execute_active_handler();
我试图从高层次上理解 Drupal 的核心系统如何工作,特别是与基于 Web 的 MVC 的关系。因此,在类似 Code Igniter 的系统中,以下
检查 URL,将其转换为类和操作
调用类上的操作方法,从模型加载信息,完成“业务逻辑”
信息被传递到视图层< /p>
布局系统呈现 HTML 页面
部分布局(通常是“内容区域”)由传递的信息驱动步骤 3 中的
Drupal 中的等效调度流程是什么?我理解模块系统是如何工作的,但我不太遵循 Drupal 关于数据加载和主题/布局渲染的方式/原因以及两者之间的切换发生的位置的哲学。
我意识到 Drupal 与 Web 应用程序 MVC 系统完全不同;我正在尝试了解如何进行。我意识到 Drupal 的设计目的是在不完全理解这一点的情况下成功使用。优先考虑 Drupal 7 答案,但如果有根本性的变化,欢迎使用以前版本的信息。
Assuming the minimal module install (to keep things simple), what are the core "responsibilities" of the two top level functions in Drupal's index.php
?
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
menu_execute_active_handler();
I'm trying to understand, from a high level, how Drupal's core systems work, particularly in relationship to web based MVC. So in a Code Igniter like system, the following
Examine the URL, turns it into a class and action
Calls the action method on the class, where information is loaded from models, "businessy logic" is done
Information is handed off to a view layer
Layout system renders HTML page
Part of the layout (often a "content area") is driven by the information handed off in step 3
What's the equivalent dispatch process in Drupal? I understand how the module system works, but I don't quite follow Drupal's philosophy on the how/why of data loading and theme/layout rendering, and where the handoff between the two happens.
I realize Drupal is radically different from web app MVC system; I'm trying to understand how. I realize Drupal is designe to be successfully used without fully understanding this. Preference given to Drupal 7 answers, but if there's been radical changes information from previous versions is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Berdir 和 Apemantus 已经给出了很好的答案(+1),但还有额外尝试的空间:
关于 Drupals 与 MVC 的关系,我用 这个问题的答案“Drupal 模块内部工作的隐喻”,这可能符合您对“高”的要求级别”概述。
至于顶级函数调用 - 嗯,有些事情只是三个出现,所以我建议将
theme('page, $return)
调用纳入其中,因为这将完成概述:一一:
drupal_bootstrap ()
顾名思义,这主要涉及“设置舞台”,即例如
这里重要的一点是,在这个阶段,钩子系统将调用其他模块,如果他们要求的话,让他们有机会在这个早期阶段注入自定义逻辑。虽然模块这样做并不常见,但它增加了 Drupal 的灵活性,可以满足特殊需求,例如影响用户识别过程、防止或强制缓存命中、重写请求的路径和其他“低级” ' 操作。
menu_execute_active_handler()
这与 CodeIgniter 草图中的步骤 1. 和 2. 大致匹配。它将检查请求的路径,将其与正确的回调函数匹配(包括一些“将通配符转换为参数”逻辑),并调用该回调,传递提取的(或预定义的)参数。回调通常期望返回主页面内容,但可以自由地执行其他操作,例如仅重定向请求。大多数“业务逻辑”将在这里完成,但请注意,返回的内容通常已经是标记片段,因此此阶段已经包含视图层的某些部分!
theme_page()
(通过 间接调用
theme()
函数,它添加了很多周围的“魔法”)这(非常)大致匹配通常的视图层,因为这是要返回的标记的最终组装发生的地方。但它也是页面“周围”元素组装的地方(例如菜单、标题、侧边栏等),因此在此阶段仍然存在很大的“业务逻辑”潜力。< /p>
在所有这些步骤中,挂钩系统(以及类似设计的主题系统)将提供相当多的“挂钩”点,供其他模块“订阅”(如果需要)。当被调用时,它们将获得当时正在处理的相关信息,并可以选择介入并操纵它(或者只是触发一些单独的处理)。所有这些加起来构成了一个相当灵活的系统(因为自定义模块有大量的“拦截和操作”选项),但也造成了学习 Drupal 的许多困难,例如“何时发生”的问题通常不容易回答:/
所以简短地总结一下:
Good answers from Berdir and Apemantus already (+1), but some room for an additional try:
Concerning Drupals relationship to MVC, I took a stab at the topic with this answer to a question for 'A metaphor for Drupal module's inner workings', which might fit with your request for a 'high level' overview.
As for the top level function calls - well, some things just come in threes, so I would suggest taking the
theme('page, $return)
call into the mix, as this will complete the overview:One by one:
drupal_bootstrap()
As the name suggests, this is mainly concerned with 'setting the stage', that is e.g.
An important point here is that already during this phase, the hook system will invoke other modules, if they ask for it, giving them a chance to inject custom logic in this early phase. While it is not common for modules to do this, it adds to the flexibility of Drupal that it can be done for special needs like e.g. influencing the user identification process, preventing or enforcing a cache hit, rewriting the requested path and other 'low level' manipulations.
menu_execute_active_handler()
This roughly matches steps 1. and 2. from your CodeIgniter sketch. It will inspect the requested path, match it to the proper callback function (including some 'turn wildcards into parameters' logic), and invoke that callback, passing the extracted (or predefined) parameters. The callback is usually expected to return the main page content, but is free to do other stuff like e.g. just redirecting the request. Most "businessy logic" will be done here, but note that the returned content is quite often already a Markup Fragment, so this phase includes some parts of the view layer already!
theme_page()
(Indirectly invoked via thetheme()
function, which adds a lot of surrounding 'magic')This (very) roughly matches the usual view layer, as this is where the final assembly of the markup to be returned takes place. But it is also the place where the 'surrounding' elements of a page get assembled (think menus, headers, sidebars, etc.) so there is still a lot of potential for "businessy logic" going on during this stage as well.
During all these steps, the hook system (along with the similarly designed theme system), will provide quite a number of 'hook in' points for other modules to 'subscribe' to, if they need to. When invoked, they will get passed the relevant information being processed at that point, with the option to step in and manipulate it (or just trigger some separate processing). All this adds up to a quite flexible system (because of the huge amount of 'intercept and manipulate' options for custom modules), but is also responsible for a lot of the difficulties in learning Drupal, as the question of 'what happens when' is often not easily answered :/
So to sum it up shortly:
看看 http://drupaldepth.blogspot.com/2009/07 /flow-of-drupal.html
Have a look at http://drupaldepth.blogspot.com/2009/07/flow-of-drupal.html
引导程序之后,唯一发生的事情是菜单路由器系统,它确定哪个页面回调负责该请求。为此,它依赖于所有已安装模块在其 hook_menu() 定义(它存储在数据库中,并且仅在明确请求时更新,例如启用新模块时)。
该钩子可以控制很多。例如访问权限、定义参数、菜单链接的标题等。此外,菜单路由器几乎可以像您想要的那样复杂,它们不限于通常的控制器/操作模式。我认为您可以定义最多 9 个元素深的菜单路由器项,例如“yourmodule/view/%/sub/%/%/whatever”。
请参阅 http://drupal.org/node/109131 以获取简短概述和示例。
在该页面回调中,提供模块可以自由地做任何它想做的事情。如果愿意的话,它可以使用主题系统、触发钩子或对页面的“内容”部分执行任何其他操作。主题系统将包括主题页面的所有其他部分,包括其他区域和自动包含在这些区域中的其他块。在创建响应 AJAX 请求或提供 xml feed 等内容的回调时,记住这一点很重要。在这些情况下,您需要采取进一步的步骤在主题中进行调整,以适应不应包含“其余”内容的请求页面。
After the bootstrap, the only given thing that happens is the menu router system, which figures out which page callback is responsible for this request. To do that, it relies on the information that all the installed modules returned in their hook_menu() definition (this is stored in the database and only updated when explicitly requested to do so, when new modules are enabled for example).
That hook can control a lot. For example access permissions, defining arguments, title of the menu links and so on. Also, menu routers can be almost as complex as you want, they are not limited to the usual controller/action patterns. I think you can define menu router items up to 9 elements deep, for example 'yourmodule/view/%/sub/%/%/whatever'.
See http://drupal.org/node/109131 for a short overview with an example.
Inside that page callback, the providing module is free to do whatever it wants. It can use the theme system if it wants to, fire hooks or do anything else with the "content" portion of a page. The theme system will include all other portions of a themed page including other regions and other blocks that are included in those regions automatically. This is important to remember when creating callbacks that respond to AJAX request or provide things like xml feeds etc. In those cases you would need to take further steps to make adjustments in the theme to accommodate requests that shouldn't include the "rest" of the page.