user1592129
user1592129

Reputation: 493

Custom config file for components

I am interested in creating a config.cfc which I want to use in differenct components.

in PHP one can create a config.php file which simply return an array. and in other php files this can be included like

use config.php

Can I simple include a .cfm file in any .cfc component? of a config.cfc which simply returns a STRUCT?

Upvotes: 0

Views: 358

Answers (2)

Scott Jibben
Scott Jibben

Reputation: 2287

I have seen several projects that use a .cfm file as a config file and it sets a Coldfusion struct variable with setting values. Using cfinclude will then load the file and set a config variable (usually a struct). It could just as easily set an array although I think structs would be more flexible. There is usually logic in the code to cfinclude the config.cfm file once and store the setting in the application scope.

Another option is to use a .json file that contains the same kind of thing but in JSON format. Here's an example of an open source project that does that:

https://github.com/tonyjunkes/CFFormProtect-Revamp/blob/master/cfformprotect/config.json

The controlling code reads the file and uses deserializeJSON() to convert it to a ColdFusion struct. Since it is open source you could download this project and see exactly how it is working.

Yes, you can cfinclude a .cfm from a .cfc file.

Upvotes: 2

Guest
Guest

Reputation: 196

I'm not sure how to answer your question because I don't fully understand what you're trying to accomplish. In one sentence you need to return an array and in another sentence you need to return a struct. If you're looking to create a config.cfc your method(s) can return either datatype (array or struct).

To answer your other question, yes you can include a .cfm file within a .cfc. I've done it in the past, although it's not best practice.

What I would suggest instead, in your config.cfc, create any needed methods then use CreateObject() in your calling .cfm or .cfc for usage.

Upvotes: 3

Related Questions