Invoking CF component without mapping

Is it possible to invoke a component (sub directory components) from within another sub directory without mapping?

Thank you in advance

On a side note, you can also use cfimport to create a namespace of a folder containing custom tags. It works like so:


<!--- create the namespace --->
<cfimport taglib="/path/to/your/directory" prefix="myTags">

<!--- use the tags --->
<myTags:tagOne attribute1="some value"  attribute2="another value">

<myTags:tagTwo>

Glad you got the mapping stuff sorted.

Probably need to see some code for your new issues ;). I’d also suggest starting a new thread for it. Just easier for people to pick it up from the main listing.

Cheers,
James

Hi James. Thank you for that. I’m going to ask you a really dump question though. I never used this before and even just migrated from Aplication.cfm to Application.cfc so this is all new to me. When I take the code in the example:


<cfcomponent>

<cfset this.mappings["/mymap"] = getDirectoryFromPath(getCurrentTemplatePath())>

</cfcomponent>

should /myMap be my components folder, i.e. /components? And if so can I than call the component from a template in subfolder like:


<cfscript> 
    siteCFC      =  createObject("component", "components.cfcSite"); 
    getTypes     =  siteCFC.getTypes("#application.dsn#");
    getLocations =  siteCFC.getLocations("#application.dsn#");
    getFeatured  =  siteCFC.getFeatured("#application.dsn#");
</cfscript>

Thank you in advance.

No worries. All had to learn this stuff at some point :wink:

In your Application.cfc do the following


<cfdump var="#getDirectoryFromPath(getCurrentTemplatePath())#" />

What do you get?

I’m guessing that’ll give you the application’s root directory path so what I think you’ll need is something like this…


<cfset this.mappings["components"] = getDirectoryFromPath(getCurrentTemplatePath()) & "components" />

Also worth dumping out …


<cfdump var="#this#" />

… in your Application.cfc as that’ll show you the current setup.

Make sure to restart ColdFusion when you change anything. Mappings are saved in perm. memory for your application so the only way to change them is to overwrite then, delete them or restart ColdFusion.

There are easier ways of doing that as well but one step at a time :wink:

Not a stupid question as you’re right :wink:

I mostly use “com” to reference my component but yeah by giving the mapping a reference name of “component” ColdFusion will look to use the directory path you’ve specified there to find your components.

Only thing to keep in mind is this technique is application-specific. Mappings in the CFAdmin are global to every application running within ColdFusion, these ones will only work for files that are running within the same application - Hope that makes sense.

BTW in your code example you don’t need the application.dsn in quote marks or hashes. You’re calling a variable so you can simply write it as this :wink:


[INDENT][/INDENT]getTypes = siteCFC.getTypes(application.dsn);

Also, “food for thought”. You could do this


[INDENT][/INDENT]siteCFC =  createObject("component", "components.cfcSite").init(application.dsn);

site.getLocation();
site.getFeatured();
site.getTypes();

Where init() is what they call a constructor (Google it ;)). You load the application.dsn value into the CFC on start up and it stores it internal. That way you don’t need to reference it everytime you call one of its functions as the CFC can just use the value it was previously given.

It’s also worth thinking about just using the function calls when you need them rather than saving the results in a variable. More OOP :slight_smile:

Cheers,
James

Hi James,

Forget this part :slight_smile:

[B]It gave me an error stating:

Invalid request of Application.cfm, Application.cfc, or OnRequestEnd.cfm file.

so i gave it temporary a differnt name and this is what the struct gave me:[/B]

I saw what was happening when I opened any other page it gave me

/components   C:\\inetpub\\wwwroot\\Andrianopoylos\\

So do I understand it right then that, as you earlier suggested, the mappings should be:


<cfset this.mappings["components"] = getDirectoryFromPath(getCurrentTemplatePath()) & "/components" />

Thank you again

It’s likely your getDirectoryFromPath(getCurrentTemplatePath()) for the mapping is wrong. That should be pointing at the components folder.

Dump out that value and adjust it to match where that CFC folder is

Hi James, sorry to bother you, hopefully for the last time, one more time :confused: I never had to use this kind of things. I used an example from Adobe and placed it in the components folder and this was the result:

The current directory is: C:\inetpub\wwwroot\Andrianopoylos\components\

So what do I have to change in


<cfset this.mappings["components"] = getDirectoryFromPath(getCurrentTemplatePath())>

Thank you in advance
donboe

Okay! I have folder with components. Than I have two or three more folders with files that should be able to call the components. All folders are in in the root of the application!

The reason that I can’t ( can’t is maybe a big word) use Mappings is that I don’t have direct access to the CF administrator.

I’m running CF8. How would I do such from Coldfusion?

Thank you so much

I’m running CF8. How would I do such from Coldfusion?

You want to have a look at this.mappings

A good article on it here - http://samfarmer.instantspot.com/blog/2007/6/1/ColdFusion-8-Per-App-Dynamic-Mappings-and-Custom-Tag-Paths-Create-Dropable-Applications

Yeah it’s common, especially in shared hosting, for the Admin to be locked down. Pain in the **** but I guess that was one of the main reasons to introduce this.mappings.

A few other articles on Google about it as well :wink:

Cheers,
James

You can invoke it if it’s in the same folder as the file calling it but apart that it’s done through mappings.

Is there are reason you can’t use mappings? It’s normally a good idea to at least have one in your application pointing at the root of your project / application.

What version of CF are you running? Mappings can be coded into your application directly from version 8.

What’s an example of the folder structure you’re trying to use?

Thank you for that, very useful :slight_smile:

Little update on this! I think I’m near to the solution :slight_smile: because the pages from the shared folder are opening but I get error messages like:

Variable PROPERTY_LOCATION_NAME is undefined.

This is a value I have in a dropdown list. The strange thing is though that I don’t get an error on PROPERTY_LOCATION_ID, which is the value in the same dropdown list. Any ideas?

Hi James. It is sorted completely. I guess I was so tired after trying and trying that I didn’t see some obvious errors. Thank you for you time and patience. :slight_smile:

cheers,
Donald

No that won’t work…

What does this give you?


<cfcomponent>
 
<cfset this.name="ANDRIANOPOYLOS">
<cfset this.sessionManagement="true">
<cfset this.clientManagement="true">
<cfset this.setClientCookies="true">
<cfset this.applicationTimeout=CreateTimeSpan(2,0,0,0)>
<cfset this.sessionTimeout=CreateTimeSpan(0,0,20,0)>
<cfset this.mappings["/components"] = getDirectoryFromPath(getCurrentTemplatePath())>
 
<cffunction name="onApplicationStart" returnType="boolean" output="false">
<cfset application.dsn = "Andrianopoylos">
 
<cfreturn true>
</cffunction>

<cffunction name="onRequestStart" returnType="any" output=true>
<cfdump var="#this.mappings#" />
<cfabort/>
<cfreturn />
</cffunction>
</cfcomponent>


Also have a look at this, a template for Application.cfc, as it’ll help you get your head around Application.cfc a bit more…

http://www.coldfusionjedi.com/downloads/application.cfc.txt

Hi James,

I’m still struggling to make this work. I did what you said (by the way a very good tip the

.init(application.dsn)

but I keep getting the error:

Could not find the ColdFusion Component or Interface components.cfcSite

The component cfcSite is in the folder components and I’m calling it from a template in a folder called shared. Both folders are in the root of this application.

What I have in my Application.cfc so far is this:


<cfcomponent>

<cfset this.name="ANDRIANOPOYLOS">
<cfset this.sessionManagement="true">
<cfset this.clientManagement="true">
<cfset this.setClientCookies="true">
<cfset this.applicationTimeout=CreateTimeSpan(2,0,0,0)>
<cfset this.sessionTimeout=CreateTimeSpan(0,0,20,0)>
<cfset this.mappings["/components"] = getDirectoryFromPath(getCurrentTemplatePath())>

<cffunction name="onApplicationStart" returnType="boolean" output="false">
<cfset application.dsn = "Andrianopoylos">

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

Like I said before I just migrated ( Or at least I try to :slight_smile: ) to Application.cfc so that’s why it still looks empty and maybe It’s not even good what I’m doing. Can you please explain where I’m going wrong with this?

Hi James,

This is driving me insane :mad:. I added one of the dumps just after the closing component tag


<cfcomponent output="false">
<cfset this.name="ANDRIANOPOYLOS">
<cfset this.sessionManagement="true">
<cfset this.clientManagement="true">
<cfset this.setClientCookies="true">
<cfset this.applicationTimeout=CreateTimeSpan(2,0,0,0)>
<cfset this.sessionTimeout=CreateTimeSpan(0,0,20,0)>
<cfset this.mappings["components"] = getDirectoryFromPath(getCurrentTemplatePath()) & "components" />
</cfcomponent>
<cfdump var="#this#" />

and this is what I get for all the hard work :slight_smile: :

Invalid CFML construct found on line 17 at column 1.
ColdFusion was looking at the following text:
<

What am I doing wrong?