Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Components mapping in Railo

I would like to put all of my CFC into /components folder and be able to call them from different places in application eg. from /forums/index.cfm.

How would I go about setting the mappings?

like image 889
Eleeist Avatar asked Aug 22 '12 12:08

Eleeist


2 Answers

Summary...

There are multiple ways to setup a mapping, and indeed two different types of mapping:

  • A traditional mapping is created via the admin, and can be used anywhere within your CFML code. Railo supports per-context and per-server mappings of this type.

  • There is also a per-application mapping, created either in your Application.cfc or via cfapplication tag, which can be used in most places, but are a runtime construct, so cannot be used at compile-time.

Also if you have global components, you might want to avoid using a mapping and simply tell Railo where your components are so you can access them directly.

Creating a per-context mapping:

Go to the Railo Admin (i.e. http://domain/railo-context/admin/web.cfm ) and in the menu just over half way down you'll find "Archives & Resources", within which is "Mappings".

In the Virtual column enter /components and in the Resource column enter the absolute path to that directory (e.g. /home/user/public_html/components ), then press the save button.

You can also create a per-context mapping programmatically, using the cfadmin tag with the action "updateMapping".

(Everything here also applies to per-server mappings, except using the Server Admin instead of the Web Admin. Per-server mappings are visible but read-only in the Web Admin interface.)

Creating a per-application mapping:

To create a mapping only for a specific application, you can do this in Application.cfc

Simply create a variable called this.mappings which contains a struct of your virtual and resource values, for example:

This.Mappings = { '/components' : '/home/user/public_html/components' }

This mapping then will only apply for that application, allowing you to have the same mapping point to different locations for different applications.

Railo also allows per-application mappings to be created inside Application.cfm, by using the cfapplication tag - like so:

<cfset MappingStruct = { '/components' : '/home/user/public_html/components' } />
<cfapplication mappings=#MappingStruct# />

Note: application-level mappings have some restrictions, since they exist at runtime, but not at compile-time (so, for example, they can't be used for custom tag libraries, where the taglib attribute is evaluated when the template is compiled).

Referencing a component with a mapping:

Either of these will enable you to do:

MyObj = createObject('component','components.NameOfFile').init()

Or:

MyObj = new components.NameOfFile()

(Note that you don't use /component/nameoffile like you might when otherwise using a mapping.)

Global components without a mapping:

If you want to access the components globally without a mapping, goto the Components section (just below Mappings in the menu), and enter your absolute components path in the "Additional Resources" section. (Leave Trusted unticked.)

Then you can simply create your objects without the components. mapping being needed:

MyObj = new NameOfFile()
like image 73
Peter Boughton Avatar answered Nov 13 '22 12:11

Peter Boughton


You should define your mappings in Application.cfc. Check out these related questions

railo application.cfc this.mappings not working

How can I include mappings into Application.cfc from external property file?

like image 40
danmcardle Avatar answered Nov 13 '22 13:11

danmcardle