Reputation: 16906
I am currently updating some of my Coldfusion applications, and I am looking for a good way to keep some of the structure in place.
Currently, its setup like this
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)
Currently, the setup works with an Application file in each directory (and it does work), but it gets messy by declaring everything like application/session scopes over again. Is there a better way?
Upvotes: 2
Views: 467
Reputation: 29870
In the Application.cfc
in the admin subdir, extend the one in the parent dir, eg:
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
}
In your base dir, add ApplicationProxy.cfc
, thus:
component extends="Application" {
}
The reason for this is that a sub Application.cfc
cannot have extends="Application"
, because that seems like a circular reference. However there's no better "qualified" way of identifying an Application.cfc in the base dir, so one needs a proxy.
Upvotes: 4
Reputation: 2616
You can also just put a check to see what directory the user is executing and then just have the extra code run. So, in your main application.cfc onRequestStart() for example, you can have say, "is the current request for the admin folder?" Ok, run function X which includes all your security functionality. You can even include this code as extra functions within your application.cfc if you like.
Alternatively, if you go with one of the other answers by doing the extends stuff, you'll want to put a super.function() call in each function where you want other code to run. For example with onRequest start, super.onRequestStart() at the beginning of your sub-Application.cfc onRequestStart() to call the parent stuff before the child fires off.
I personally prefer the first method as it keeps it tidy when you truly do have just one application.
Upvotes: 0
Reputation: 1755
I would try something like this in your Application.cfc on RequestStart():
<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>
Then, in each directory you want to have custom variables set up, put a admin.cfm file. In this admin.cfm file just put a bunch of tags or however you want to set up your session and application variables.
Upvotes: 0