How to extend application.cfc without overwriting parent functions?

I conducted several searches on the Internet to find out if this could be done, and did not find anything that could tell. I have application.cfc at the root and there are several subfolders where I want its functions to be the same with minor differences. For example, cfc has a root function onRequestStart (), which includes a function to determine the relative root path for the called template (that is, whether it should be "../" or "../../", etc. , see http://www.bennadel.com/blog/1655-ask-ben-dynamic-web-root-and-site-url-calculations-in-application-cfc.htm ).

I want this to be executed on all pages in all subfolders, but in one subfolder I also want to run a security scheme for each request that checks if the user has valid permissions to view pages in this subfolder. If I create application.cfc in this subfolder that extends cfc in the root, then any onRequestStart () function that I place there will overwrite the parent onRequestStart ().

Right now, I decided that the only way to make child onRequestStart () "the same as the parent, with a little extra" is to copy the parent cfc onRequestStart () to the child and add extra protection inside. However, this will cause the function to freeze, which finds the relative length of the web pages. For pages in this subfolder, "../../" will refer to application.cfc in the subfolder, and not to the parent root in the root, which is what you need.

My hacky way around this was to put the code in the parent cfc onRequestStart ():

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

, -. , , , . ?

+4
2

Google-foo, , ;) , . , Application.cfc, () SUPER.

- : Application.cfc OnRequestStart() SUPER. :

: . SUPER , ColdFusion . , " " "". SUPER ( , ), , , ColdFusion ( ).

, Application.cfc ColdFusion, SUPER OnRequestStart() Application.cfc .cfc. , Application.cfc, , :

, , . .

Application.cfc SUPER:

<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 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>
+3

, "application.cfc", "child", "extends" "subfolder", Ben , .

, ​​ , - , , extends cfcomponent cfc, CF "extends =" ", "application.cfc", , cfc .

- , cfc , , "application.cfc", application.cfc . , - . , cfc "applicationProxy.cfc" ( , ) , :

<cfcomponent displayName="applicationProxy" extends="Application"></cfcomponent>

application.cfc :

<cfcomponent extends='applicationProxy'...
0

Source: https://habr.com/ru/post/1650809/


All Articles