可以在 application.cfc 中创建无作用域全局吗?
我正在将使用 application.cfm 的旧应用程序迁移为使用 application.cfc。 CFM 设置了一些全局变量,例如
<cfset dsn = "myDSN">
我尝试将该行代码放入 onApplicationStart、onRequestStart 等中,但尝试在测试页中打印该值会导致错误。当然,在应用程序范围(例如 application.dsn)中设置一个值效果很好,但我的截止日期很紧,无法通过对每个全局进行站点范围的搜索和替换来破坏一切。
我知道将这些放在作用域中是正确的做法,但目前是否有任何方法可以切换到使用 Application.CFC 但仍创建无作用域的全局变量?
I'm migrating an old app that uses application.cfm to use an application.cfc. The CFM sets a few globals such as
<cfset dsn = "myDSN">
I've tried putting that line of code in onApplicationStart, onRequestStart, etc. but trying to print that value out in a test page results in an error. Setting a value in the application scope (e.g. application.dsn) works fine of course but I'm under a tight deadline and can't rock the boat by doing a site-wide search and replace for each global.
I know putting these in scopes is the right thing to do but, for the time being, is there any way to switch to using Application.CFC but still create unscoped global variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将其放入
onRequest()
而不是onRequestStart()
中。Try putting it inside
onRequest()
instead ofonRequestStart()
.您将需要 Application.cfc 和 Application.cfm。将所有主要代码从 Application.cfm 移至 Applicaiton.cfc 的适当部分。
然后,这可能是 Applicaiton.cfm 中唯一的一行:
现在返回到 Application.cfc 并将其添加到所有函数:
在 test.cfm 上,现在应该能够输出不带作用域前缀的 dsn 变量:
如果在 CFC 内部的任何位置定义变量.dsn,则将该变量放入 CFC 的变量范围内,该范围是 CFC 私有的。通过在 CFM 中定义它,然后包含 CFM,它就保存在页面请求的变量范围内。
You'll need both Application.cfc and Application.cfm. Move all of the major code from Application.cfm to the proper portions of Applicaiton.cfc.
Then, and this may be the only line, in Applicaiton.cfm:
<cfset variables.dsn = "myDSN" />
Now go back to Application.cfc and add this outside of all of the functions:
<cfinclude template="Application.cfm" />
On your test.cfm, should now be able to output the dsn variable without the scope prefix:
<cfoutput>#dsn#</cfoutput>
If you define variables.dsn anywhere inside of the CFC, you're putting the variable into the variables scope of the CFC, which is private to the CFC. By defining it in the CFM, then including the CFM, it's kept in the variables scope of the page request.
经过我自己的努力,我会给你我的解决方案。请注意,这适用于 CF9,我不知道其他版本是否有效(以前版本中的 onRequest 和 AJAX 调用存在问题)。基本上,Application.cfc 中必须有 onRequest 函数。这是一个基本模板:
Having wrestled with this myself, I'll give you my solution. Note that this works on CF9, I don't know about other versions (there is an issue with onRequest and AJAX calls in previous versions). Basically, it's necessary to have the onRequest function in your Application.cfc. Here's a basic template: