Application.cfc将值扩展到子文件夹中

时间:2012-10-01 16:31:27

标签: coldfusion application.cfc

我目前正在更新我的一些Coldfusion应用程序,我正在寻找一种保持一些结构的好方法。

目前,它的设置如下

ApplicationRoot/Application.cfc (handles things like login, init etc..)
ApplicationRoot/Admin (I want exact same var's as parent folder, but few extra checks to ensure the user has admin rights)

目前,安装程序与每个目录中的应用程序文件一起使用(并且确实有效),但通过再次声明应用程序/会话范围等所有内容会使其变得混乱。还有更好的方法吗?

3 个答案:

答案 0 :(得分:4)

在admin子目录的Application.cfc中,扩展父目录中的那个,例如:

component extends="ApplicationProxy" {

    // eg: if you need to do something different in the local onApplicationStart:
    public void function onApplicactionStart(){
        super.onApplicationStart();
        // stuff that's different from the parent goes here
    }    

    // if there's nothing different for a given handler, then don't have one in here: the super one will still fire

    // otherwise override each handler in a similar fashion to the onApplicationStart above, with:
    // a) a call to its super equivalent
    // b) anything that needs overriding

}

在你的基础目录中,添加ApplicationProxy.cfc,因此:

component extends="Application" {

}

原因是子Application.cfc不能有extends="Application",因为这似乎是一个循环引用。但是,没有更好的“合格”方法来识别基础目录中的Application.cfc,因此需要代理。

答案 1 :(得分:0)

我会在RequestStart()上的Application.cfc中尝试这样的事情:

<cffunction name="onRequestStart" returnType="boolean" output="false" hint="I handle page requests." >
  <cfargument name="requestname" type="string" required="true" >

  <cfif FileExists(GetDirectoryFromPath(arguments.requestname) & '/admin.cfm')>
    <cfinclude template="#GetDirectoryFromPath(arguments.requestname)#/admin.cfm">
  </cfif>
</cffunction>

然后,在每个目录中要设置自定义变量,请输入admin.cfm文件。在这个admin.cfm文件中只是放了一堆标签,或者你想设置你的会话和应用程序变量。

答案 2 :(得分:0)

您也可以只检查用户正在执行的目录,然后运行额外的代码。因此,在您的主application.cfc onRequestStart()中,您可以说,“是当前对admin文件夹的请求吗?”好的,运行功能X,其中包括您的所有安全功能。如果愿意,您甚至可以在application.cfc中将此代码作为额外函数包含在内。

或者,如果您通过执行扩展内容来获得其他答案之一,则需要在每个要运行其他代码的函数中调用super.function()。例如,使用onRequest start,super.onRequestStart()在您的子Application.cfc onRequestStart()的开头,在子项触发之前调用父项。

我个人更喜欢第一种方法,因为当你真正只有一个应用程序时,它会保持整洁。

相关问题