PDA

View Full Version : coldfusion link problem



bob_
June 4th, 2012, 03:21 PM
I am using cfinclude and having a hard time figuring out a relative path solution it says template this is my code
application.cfc file

<cfcomponent output="false">
<cfset This.application.basePath= GetDirectoryFromPath(CGI.SCRIPT_NAME) />

Then on one of my webpages lets say index.cfm/users/userinfo.cfm i use


<cfinclude template="#application.basePath#header.cfm">

and i would get an error saying "Element BASEPATH is undefined in APPLICATION." I know that I can use "../" to get the template but then when i go to another webpage that is not listed on that folder i get an error also. How can i use relative path in my cfc to apply to all of the cfincludes ?

jakeh
June 4th, 2012, 03:51 PM
Hi,

There are a few issues:

1) The Application variable (basePath) needs to be set inside the onApplicationStart() method of the Application.cfc
2) You will need to remove "this." from before "application.basePath"
3) Because GetDirectoryFromPath() returns a String, you'll need to evaluate that string inside hashmarks, and put quotations around the evaluation of the function.

As such, the Application.cfc should look something like this:

<cfcomponent output="false">
<cfset THIS.Name = "cftestapp" />

<cffunction name="onApplicationStart" returnType="boolean" output="false">
<cfset application.basePath = "#GetDirectoryFromPath(CGI.SCRIPT_NAME)#" />

<cfreturn true>
</cffunction>
</cfcomponent>

Then you'll want to force the Application to restart (http://forum.hostek.com/showthread.php?646-How-to-restart-your-ColdFusion-application) so ColdFusion will run your onApplicationStart() method.

When I tested this, the <cfinclude> worked as expected.

Take care!

bob_
June 4th, 2012, 10:16 PM
thank you very much.