ColdFusion Application.cfc - 执行顺序 [英] ColdFusion Application.cfc - order of execution

查看:13
本文介绍了ColdFusion Application.cfc - 执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个现实检查 - 并希望一个解释(如果我的现实是错误的).

I need a reality check - and hopefully an explanation (if my reality is wrong).

CF应用框架评估事物的方式是这样的(我的理解)- 请求被传递给cfserver

The way the CF application framework evaluates things is this (my understanding) - request is passed to cfserver

  • cf 查找 application.cfm 或 cfc(基于遍历规则)

  • cf finds an application.cfm or cfc (based on traversing rules)

application.cfc 执行(如果找到)

application.cfc executes (if found)

THIS 范围已设置(可以在此处设置一系列特定于应用程序的变量,但

the THIS scope is set (a series of application specific vars can be set here but

有些是必需的 - 例如applicationTimeout" - 然后会发生一系列事件 - 并在需要时触发方法.

some are required - such as "applicationTimeout" - then a series of events takes place -and methods fired if needed.

--onApplicationStart()

-- onApplicationStart()

----onSessionStart()

----onSessionStart()

-----onRequestStart()

------onRequestStart()

等等

所以我的问题

1) THIS 设置发生在每个页面请求上 - 在其他任何事情之前?

1) The THIS settings happens on EVERY page request - before anything else?

2) 如果我在 onApplicationStart() 中设置了一个应用程序变量 - 它在之后发生的任何进程中都可用 - 并且应该在内存中持续存在 applicationTimeout() 的长度 - 正确吗?

2) If I set an application variable, in onApplicationStart() - it is available in any process that happens after that - AND should persist in memory for the length of applicationTimeout() - correct?

3) 所以如果我做这样的事情......

3) so if I do something like this...

if ( isdefined("application.myvar" ) {this.something = application.myvar;}

if ( isdefined("application.myvar" ) { this.something = application.myvar; }

它应该在启动应用程序范围的初始请求之后处理任何页面请求.

it SHOULD work on any page request after the initial request that started the application scope.

但它似乎没有这样做.

我问这个的原因是 - 有一些有趣的应用程序杠杆设置需要在这个范围内设置......其中一些可能是密集的"(至少从执行每个请求的角度来看 -所以我只想做一次,在持久内存中设置一个结构,然后将它们作为 THIS 使用.

my reason for asking is this - there are some interesting application lever settings that need to be set in the THIS scope... a few of them could be 'intensive' (at least form the perspective of executing on EVERY request - so I want to do them only ONCE, set a structure in persistent mem, and then have those available as THIS.

我是否做出了一些错误的假设?

am I making some wrong assumptions?

谢谢

推荐答案

这里有两件事在起作用:代码何时运行,变量作用域何时可用以及它们的持续时间.

There's two things at play here: when code runs, and when variable scopes are availed and how long they last.

  • 没有任何方法的代码(即:伪构造函数")运行每个请求.显然尽量减少这部分 CFC 中的代码量.
  • 各种事件处理程序中的代码按照事件处理程序名称的指示运行,例如:onApplicationStart() 代码仅在应用程序启动时运行一次.同上 onSessionStart() 每个新会话仅运行一次.

范围:

  • 此范围在整个 CFC 中可用.行为与任何其他 CFC 中的 this 范围完全相同,除了一些 this 范围的变量具有特殊含义(如 this.namethis.datasource 等).这些特殊含义的变量可以在每个会话或相关处理程序中的每个请求中更改,但似乎适用于整个系统(即:不适用于特定会话或进行设置更改的请求).在普通的 CFC 中,this 范围用于公开公共变量,但由于没有 Application.cfc 的公共实例,因此使用 this 范围没有任何意义那些特殊的设置.如果希望 CFC 中的所有方法都可以使用变量,请像往常一样使用变量范围.与此处某人的建议相反,此范围的变量与应用范围的变量不同.
  • 请求范围在整个 CFC 中也可用(伪构造函数和方法).这些也可用于稍后在请求中调用的模板的调用代码,就像任何其他请求范围的变量一样.
  • 应用范围:在伪构造函数中不可用,即使在 this.name 设置之后.仅在 onApplicationStart() 中可用,此后从那里可用.
    • 会话范围:类似地,在伪构造函数或 onApplicationStart()l 中不可用,直到 onSessionStart() 才可用.
    • this scope is available throughout the CFC. Behaves exactly like the this-scope in any other CFC, except some this-scoped variables have special meaning (like this.name, this.datasource, etc). Those special-meaning variables can be changed per session or per request in the relevant handlers, but seem to apply to the entire system (ie: not for the specific session or request making the setting change). In normal CFCs, the this scope is used to expose public variables, but as there is no public instance of Application.cfc, there is no point using the this scope beyond making those special settings. If one wants to have variables available to all methods within the CFC, use the variables scope as one normally would. Contrary to someone's advice here, this-scoped variables are NOT the same as application-scoped variables.
    • The request scope is also available throughout the CFC (pseudo constructor and methods). These will also be available to the calling code of templates called later in the request, like any other request-scoped variables.
    • Application scope: not available in the pseudo-constructor, even after the this.name setting is made. Only becomes available in onApplicationStart() and thenceforth from there.
      • Session scope: similarly, not available in the pseudo-constructor or onApplicationStart()l and not until onSessionStart().

      我在一篇博文(提供了测试代码)中对此进行了演示,在这里.篇幅太长,这里就不一一列举了,不过上面的东西总结了一下.

      I've demonstrated this in a blog post (test code provided), over here. it's too lengthy to include here, but the stuff above summarises it.

      这篇关于ColdFusion Application.cfc - 执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆