2

I am adding Coldbox to our legacy application and I ran into a problem where we can't access certain variables from within the views when using Coldbox. In the existing legacy code inside of Application.cfc in the onRequestStart method we set several variables like so:

VARIABLES.screenID  = 0;
VARIABLES.DSN               = 'datasourcemain';
VARIABLES.DSNRO             = 'datasourcereadonly';
VARIABLES.DSNADMIN          = 'datasourceadmin';
VARIABLES.pagetitle         = "Default Page Title for web application";

This is just a small snippet of the variables set. The problem is that in the legacy code these were used all over the place like in the header and footer. When browsing to a legacy page these are still accessible but when sending the request through coldbox the variables become inaccessible. My question is, is there a recommended way that I can have Coldbox know about these variables and pass them along to the views so I don't have to modify hundreds of files?

Yamaha32088
  • 4,125
  • 9
  • 46
  • 97

1 Answers1

4

It depends, there are a few places to define such variables. From the limited information given, I would suggest you add the datasource information to the Coldbox.cfc > datasources struct (#1) and add the default pageTitle to the global request handler (#2). As for the screenID, who knows -- good luck!

  1. config/Coldbox.cfc has both a settings and datasources struct that can be injected via wirebox into the handlers/controllers.

    // Dependency Injection using WireBox 
    property name='settings' inject='coldbox:settings';
    
  2. Use a global request handler and add all the global vars into the prc (private request context) which is visible to the controller and views.

    //config/Coldbox.cfc
    ...
    coldbox = {
      requestStartHandler: 'Main.onRequestStart'
    };
    ...
    
    // handlers/Main.cfc
    component extends='coldbox.system.EventHandler' {
      function onRequestStart( event, rc, prc) {
        prc.screenID  = 0;
        prc.DSN               = 'datasourcemain';
        prc.DSNRO             = 'datasourcereadonly';
        prc.DSNADMIN          = 'datasourceadmin';
        prc.pagetitle         = "Default Page Title for web application";
      }
    }
    
  3. Use a request interceptor and add data to the prc.

    //config/Coldbox.cfc
    ...
    interceptors = [
      { class="interceptors.Globals" }
    ];
    ...
    
    //interceptor/Globals.cfc
    component {
      property name='legacyGlobals' inject='LegacyGlobals';
    
      function preProcess(event, interceptData) {
        event.setPrivateValue('someLegacyGlobalVar', legacyGlobals.getSomeLegacyGlobalVar() );    
      }
    }
    
Cory Silva
  • 2,761
  • 17
  • 23