将通用 udf 分配给 application.cfc 的正确方法是什么?
我只想在 application.cfc 中定义一个函数并将其公开给所有请求。优选地,“分配”仅在应用程序启动时发生。
是按照以下方式做某事的首选方法:
<CFCOMPONENT OUTPUT="FALSE">
<CFSET this.name = "Website">
<CFSET this.clientManagement = true>
<CFSET this.SessionManagement = true>
<CFFUNCTION NAME="GetProperty" OUTPUT="False">
<CFARGUMENT NAME="Property">
<CFRETURN this.Props[Property]>
</CFFUNCTION>
<CFFUNCTION NAME="OnApplicationStart" OUTPUT="FALSE">
<CFSET Application.GetProperty = GetProperty>
.
.
.
还是有更好的方法?
I simply want to define a function in application.cfc and expose it application wide to all requests. Preferably the "assignment" would only happen on application startup.
Is the preferred method to do something along the lines of this:
<CFCOMPONENT OUTPUT="FALSE">
<CFSET this.name = "Website">
<CFSET this.clientManagement = true>
<CFSET this.SessionManagement = true>
<CFFUNCTION NAME="GetProperty" OUTPUT="False">
<CFARGUMENT NAME="Property">
<CFRETURN this.Props[Property]>
</CFFUNCTION>
<CFFUNCTION NAME="OnApplicationStart" OUTPUT="FALSE">
<CFSET Application.GetProperty = GetProperty>
.
.
.
or is there something better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
默认情况下,GetProperty 将在变量范围中可见,这对于许多用途来说已经足够了(在 .cfm 模板中)。
如果您想直接在组件中使用这些方法,那么在 Application 范围内引用它们就可以了。
虽然我在 onRequestStart() 中使用 Request 范围来执行此操作,但这只是我个人的偏好。像这样的事情:
请注意,通常的最佳实践是封装对象并在宿主对象的变量范围中引用它们。我通常在初始化对象时执行此操作,只需将先前创建的对象作为 init() 参数传递即可。
PS 现在建议标签及其属性使用小写。一种良好的编码实践。
By default, GetProperty will be visible in Variables scope already, this can be sufficient for many usages (in .cfm templates).
If you want to use these methods directly in the components, referencing them in the Application scope is fine.
Though I do this with Request scope in the onRequestStart(), it's just my personal preference. Something like this:
Please note that best practice in general is incapsulating the objects and having them referenced in variables scope of the host object. I typically do this when initializing the object, simply pass previously created objects as init() arguments.
P.S. Nowadays it is recommended to use lower case for tags and their attributes. Kind of good coding practices.
存储站点特定配置数据的最佳方法可能是创建一个名为 SiteConfig.cfc 的新组件,并使用 getProperty(propertyName) 和 getProperty(propertyName) 等方法。强>setProperty(属性名称,值)。然后,您可以通过在 Application.cfc 的 onApplicationStart 方法中执行以下操作来将此 CFC 存储在应用程序范围中,例如:
回到您最初的问题,尽管关于在应用程序范围中存储 UDF,下面是一种方法。其基础是,在 onApplicationStart 中,您将使用站点的配置属性(例如 siteName 等)创建一个新的应用程序持久结构。然后将函数存储在仅在 onApplicationStart 中包含的 CFM 文件中,然后将其复制到应用程序范围中。这意味着所有常规页面 CFM 文件都可以使用 application.getProperty(propertyName)。
由于该函数仅创建一次并存储在应用程序范围中,因此它满足您最初问题的有关“分配仅在应用程序启动时发生”的要求。
希望这会有所帮助!
getProperty.function.cfm
Application.cfc
test.cfm
The best way to store site specific config data is probably going to be to create a new component named something such as SiteConfig.cfc with methods such as getProperty(propertyName) and setProperty(propertyName, value). You would then store this CFC in the application scope by doing the following inside Application.cfc's onApplicationStart method like:
Back to your original question though about storing a UDF in the Application scope, below is a way to do that. The basis is that in onApplicationStart you will create a new application persisted struct with your site's config properties like siteName and whatever else. Then a function is stored in a CFM file which is cfincluded only in onApplicationStart, then copied into the application scope. This means that all your regular page CFM files can use application.getProperty(propertyName).
Since the function is only created once and stored in the application scope it satisfies your original question's requirements about "assignment would only happen on application startup".
Hope this helps a bit!
getProperty.function.cfm
Application.cfc
test.cfm
您可能会考虑创建一个单独的“属性”CFC 并将其实例化为
SERVER 范围内的单例,那么它可以从任何 CFML 页面使用,甚至
如果它不是应用程序的一部分。如果你走这条路,那么就没有“服务器”
要绑定到的“start”事件。相反,您可以将其放入 application.cfc 的构造函数中
或者在 application.cfm 的主体中
锁定代码是为了防止在每个上创建和分配 UDF 的开销
要求。这也允许属性实例持续存在,以便具有
properties.SetProperty() 函数将起作用
You might consider creating a seperate "properties" CFC and instanciating it as
a singleton in the SERVER scope then it will be available from any CFML page even
if it isn't part of an application. If you go this route then there is no "server
start" event to bind to. Instead you can put this in the contructor of application.cfc
or in the body of application.cfm
The lock code is to prevent the overhead of creating and assigning the UDF on every
request. This also allows the properties instance to persist so that having a
properties.SetProperty() function will work
您可能还想使用此处讨论的技术
you might also want to use the technique discussed here