gpt4 book ai didi

coldfusion - 如何在不覆盖父函数的情况下扩展 application.cfc?

转载 作者:行者123 更新时间:2023-12-04 00:03:38 25 4
gpt4 key购买 nike

我在网上做了一些搜索,看看这是否可以完成,但没有找到任何可以说的内容。我在根目录下有一个 application.cfc,并且有几个子文件夹,我希望它的功能相同,只有细微的差别。例如,根处的 cfc 在 onRequestStart() 中有一个函数,该函数结合了一个函数来确定被调用模板的相对根路径(即,它应该是“../”还是“../../”)等,见 http://www.bennadel.com/blog/1655-ask-ben-dynamic-web-root-and-site-url-calculations-in-application-cfc.htm)。

我希望它在所有子文件夹中的所有页面上运行,但在一个子文件夹中,我还想对每个请求运行一个安全方案,以检查用户是否具有查看该子文件夹中页面的有效权限。如果我在该子文件夹中创建一个 application.cfc 来扩展根处的 cfc,那么我放置在那里的任何 onRequestStart() 函数都将覆盖父级的 onRequestStart()。

现在,我在想使 child 的 onRequestStart() “与 parent 的一样,只是多一点”的唯一方法是将 parent cfc 的 onRequestStart() 复制到 child ,并在其中添加我的额外安全代码.但是,这样做会破坏查找页面相对 webroot 路径的功能。对于该子文件夹中的页面,“../../”将相对于子文件夹中的 application.cfc,而不是相对于根目录下的父目录,这正是我们所希望的。

我解决这个问题的技巧是将代码放在父 cfc 的 onRequestStart() 中:

<cfif cgi.script_name CONTAINS "/mySubFolder/">
(Test the logged-in user's session.roleId; if test fails, cflocation to page that reads "you don't have permissions to view")
</cfif>

基本上对任何其他子文件夹或子文件夹执行相同的操作,我希望为该子文件夹及其任何子文件夹运行其他代码。这在这个特定站点上没问题,因为它不是那么大,但我看到这对于更大的站点会变得非常困惑。有一个更好的方法吗?

最佳答案

你的 Google-foo 一定让你失望了 ;) Ben Nadel 有一个你正在尝试做的事情的例子。基本上,您不需要从继承的 Application.cfc 中复制该函数,您只需要使用 SUPER 调用它(调用它)。范围。

这是 Ben 文章的链接 - Ask Ben: Extending Application.cfc And OnRequestStart() With SUPER .从那篇文章:

I have one word for you: SUPER. The SUPER scope is created when one ColdFusion component extends another one. It is available to the extending component and gives access from the extending component up into the extended component, sometimes referred to as the "base component" or "super component." The SUPER scope not only allows us to get access to values in the super component (which isn't necessary since those values are also available in the extending component), but more importantly, it allows us to execute functions in the super component even when those functions are being overridden by the extending ColdFusion component (as they will be in our demo).

To leverage this and pull in the configuration data that you have in your root Application.cfc ColdFusion component, we are going to use the SUPER scope to invoke the OnRequestStart() event method of the root Application.cfc from the sub Application.cfc. But first, let's take a quick look at the root Application.cfc to make sure we are all on the same page:



我不想在这里复制整篇文章,但他继续举例说明正在发生的事情。我绝对推荐阅读他的整篇文章。

这是他使用 SUPER 的示例 Application.cfc范围:
<cfcomponent
output="false"
extends="personal.ben......app_extend2.Application"
hint="Secondary application event handler.">

<cffunction
name="OnRequestStart"
access="public"
returntype="boolean"
output="false"
hint="Hanldes pre-page processing for each request.">

<!--- Define arguments. --->
<cfargument
name="Page"
type="string"
required="true"
hint="The template requested by the user."
/>

<!---
Since this application is extending the root
Application.cfc, let's leverage the pre-processing
power of the root OnRequestStart(). Check to see
what value (true/false) that the root application
would have returned for this request.
By calling SUPER.OnRequestStart( Page ), we are
giving the root application a chance to run logic
on the page request. **Remember that the
OnRequestStart() method can return false to kill the
current page request. Therefore, we have to check to
see what value would be returned and honor that.
--->
<cfif SUPER.OnRequestStart( ARGUMENTS.Page )>

<!--- Store the sub root directory folder. --->
<cfset REQUEST.SubDirectory = GetDirectoryFromPath(
GetCurrentTemplatePath()
) />

<!--- Return out. --->
<cfreturn true />

<cfelse>

<!---
The root application returned false for this
page request. Therefore, we want to return
false to honor that logic.
--->
<cfreturn false />

</cfif>
</cffunction>

</cfcomponent>

关于coldfusion - 如何在不覆盖父函数的情况下扩展 application.cfc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38877339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com