The Scenario: My Coldbox/Coldspring/Transfer application has settings that I want to make maintainable via a user interface. Now, the Coldbox.xml.cfm file does make provision for me to add as many custom settings as I want to my app, but I don't want to require the end user to know how to edit this xml file (nor would I trust them to). I came up with a solution to this challenge that I'm really happy with, so I thought I'd share some snippets in case it proves useful to others.
Step 1 is to ensure that you have a table designed to hold these configuration settings. My database has a table named "configuration", with the fields ID, settingName, and settingValue.
CREATE TABLE [dbo].[configuration]( GO Of course, make sure this table is registered in your Transfer.xml.cfm file: Next, I had to find the equivalent of the "onApplicationStart" within my Coldbox app because it is on application start that I wish to read in and load my settings. In Coldbox, this is an interception point named "afterAspectsLoad", which runs during application startup just after all plugins are created (important in my case especially since I'm relying on the "ioc" plugin to get Transfer for me). In order to leverage this interception point, you'll need to create an interceptor and drop it in to your "interceptors" directory. Here is the interceptor I created that loads up my app's settings: As you may have noticed, I'm allowing my setting values to be JSON strings if needed, just in case I want to pass in an array or structure of values for a particular setting.
[id] [int] IDENTITY(1,1) NOT NULL,
[settingName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[settingValue] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
CONSTRAINT [configuration_PK_UC1] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
SET ANSI_PADDING OFF
<object name="configuration" table="configuration" >
<id name="id" type="numeric" generate="false"/>
<property name="settingName" type="string" column="settingName" nullable="false"/>
<property name="settingValue" type="string" column="settingValue" nullable="false"/>
</object>
</package>
hint="I load settings stored in the database"
output="false"
extends="coldbox.system.interceptor">
<cffunction name="Configure" access="public" returntype="void" hint="" output="false" >
<cfset instance.ioc = getPlugin("ioc") />
</cffunction>
<cffunction name="afterAspectsLoad" access="public" returntype="void" output="false" hint="I load in any application settings from the database">
<cfargument name="event" required="true" type="coldbox.system.beans.requestContext" />
<cfset var qryConfigSettings = instance.ioc.getBean("transfer").list("configuration.configuration") />
<cfset var tmpVal = "" />
<cfloop query="qryConfigSettings">
<cfif isJSON(settingValue)>
<cfset tmpVal = deserializeJSON(settingValue) />
<cfelse>
<cfset tmpVal = settingValue />
</cfif>
<cfset setSetting(settingName,tmpVal) />
</cfloop>
</cffunction>
</cfcomponent>
The last item of business is to let Coldbox know that our interceptor exists. We do this by registering it in the <Interceptors> section of Coldbox.xml.cfm, like so:
That's it! re-initialize your application (&fwreinit=1) and all of your settings will be available anywhere within your app.
You are not logged in, so your subscription status for this entry is unknown. You can login or register here.
No comments found.

