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?
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.
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.)
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).
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.)
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()
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With