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

标签 coldfusion application.cfc

我在网上做了一些搜索,看看这是否可以完成,但没有找到任何可以说的内容。我在根目录下有一个 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/

相关文章:

coldfusion - 自动化 ColdFusion 管理员设置的方法

coldfusion - 如何使用 CFQUERY 从 Table2 中为 Table1 中的每一项获取一个结果?

coldfusion - Application.cfc 与 Application.cfm 以及扩展 Application.cfc 的需要

session - 使用不带 cookie 的 Coldfusion session

coldfusion - 如何在不重置服务器实例的情况下重置 application.cfc?

ColdFusion 遍历具有空/未定义字段的数组

encryption - 指定的 key 不是此加密的有效 key : Key size is not valid. 获取的 key 长度为:15

ColdFusion 错误 - 我在移动到新服务器的网站上遇到错误?

冷聚变 "Routines cannot be declared more than once"

javascript - 如何在 Application.cfc 的 onRequestStart() 中包含 JavaScript