Search code examples
coldfusioncoldfusion-11

How to add path with dots in CreateObject Argument


I'm trying to import the path from my component, but one folders has multiple dots, like board.event.calendar. Since this is the regular structure in this code base, I just can't change the folder name. I have tried multiple ways to achieve the right path in CreatObject argument, such as:

<cfset Event = CreateObject("Component", "path.to.'board.event.calendar'.Event") />
<cfset Event = CreateObject("Component", "path/to/'board.event.calendar'/Event") />
<cfset Event = CreateObject("Component", "path\to\'board.event.calendar'\Event") />
<cfset Event = CreateObject("Component", "path.to.board#chr(046)#event#chr(046)#calendar.Event") />

But no luck so far. How can I manage that?


Solution

  • CreateObject() uses the period character as the separator in your path, and will not accept invalid characters such as the single quote (as in your example above).

    One way to do this is to create a mapping, and then use that in your path. For example, if the path to your application is C:\dev\myapp\, and the path to your calendar CFCs are in C:\dev\myapp\calendar\, then you can create a mapping in your application.cfc file like this:

    this.mappings = {
        "/app": "C:/dev/myapp/",
        "/calendar: "C:/dev/myapp/calendar/"
    }
    

    (You can also create mappings in the CF Administrator)

    And then when creating your component, you could use either:

    myComponent = createObject("app.calendar.event").init();
    

    or

    myComponent = createObject("calendar.event").init();