0

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?

user1592129
  • 461
  • 1
  • 5
  • 16
  • 1
    The answer by @Guest should put you on the right track. One of the main benefits of using ColdFusion components is for code reuse. Perhaps reading this article will help further explain it - [Learn CF in a week - Components](http://www.learncfinaweek.com/week1/Components/) – Miguel-F Jan 19 '18 at 15:58
  • [From what I read here](https://stackoverflow.com/questions/10965454/how-does-the-keyword-use-work-in-php-and-can-i-import-classes-with-it), `use` just " just imports a namespace into the current scope. Don't think there's an equivalent in CF. You just use the cfc path when creating an instance of the component, ie ``. Then invoke whatever method(s) of the component you want ie `` – SOS Jan 19 '18 at 17:32

2 Answers2

3

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.

Guest
  • 196
  • 3
  • I believe CF9 also supports the `new` for components, so one could also use the shorter syntax `new Config()`. – SOS Jan 19 '18 at 17:25
  • Are you sure that createobject() works with .cfm files? – Dan Bracuk Jan 19 '18 at 17:38
  • @DanBracuk - Think you misread it. Sounds like they're saying use `createObject()` *from* a cfm/cfc file, not use it *with* a cfm file. – SOS Jan 19 '18 at 17:41
  • @DanBracuk, @Ageax is correct. I was saying the calling `.cfm` or `.cfc` would use CreateObject(). – Guest Jan 19 '18 at 17:54
2

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.

Scott Jibben
  • 2,229
  • 1
  • 14
  • 22