Blog

Tip of the Week: Using Interceptors

Brad Wood December 20, 2012

Spread the word

Brad Wood

December 20, 2012

Spread the word


Share your thoughts

 

One of the most powerful parts of the ColdBox framework are interceptors.  Interception points are events that happen over the life cycle of your application or a user request which are broadcast by the framework.  Examples would be preProcess (when a request first comes in), onException (when an error happens), or preViewRender (before a view is rendered).  If you any code you want to execute at those points, or if you want to override/modify the default behavior of the framework, you simply need to register an interceptor to listen for that broadcast event.
 
Interceptors, like most everything else in ColdBox, are implemented as simple CFCs which have a configure() method that is called on their creation.  Then you create as many methods as you want named after the interception points you want to respond to.  
 
For Example:
/interceptors/GateKeeper.cfc
component{
    property name="securityService" inject="model";
 
    function configure(){}
 
    function preEvent(event,interceptData){
        if(interceptData.processedEvent == 'secure.page' && !securityService.loggedIn())  {
            setNextEvent("login.page");
        }
    }
}
 
Then, all you have to do is register your interceptor in the config like so:
interceptors = [
    {class="interceptors.GateKeeper", properties={}}
];
 
Now, the code in our preEvent method will get called upon to run before each event in your application.  This keeps cross cutting concerns nicely encapsulated in a way that can easily be turned on and off without actually touching the parts of the app that they are listening to.
 
Check out the full list of interception points here in the docs:  http://wiki.coldbox.org/wiki/Interceptors.cfm
 
P.S. As if all the built-in ColdBox interception points weren't cool enough, you can create your own custom interceptions in your app like orderCreated, userLoggedIn, or accountDeleted and then write interceptors that listen for them and do special logic.
 

Add Your Comment

Recent Entries

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Unfortunately, we often hear this message from clients who thought it would never happen to them... until it did. Some believed they could delay the expense of Implementing ColdFusion security best practices for one year, while others were tempted to put it off for just a few months. However, in today's rapidly evolving digital landscape, the security of web applications, including ColdFusio...

Cristobal Escobar
Cristobal Escobar
April 16, 2024
Ortus March Newsletter

Ortus March Newsletter

Welcome to Ortus Solutions’ monthly roundup, where we're thrilled to showcase cutting-edge advancements, product updates, and exciting events! Join us as we delve into the latest innovations shaping the future of technology.

Maria Jose Herrera
Maria Jose Herrera
April 01, 2024
Into the Box 2024 Last Early Bird Days!

Into the Box 2024 Last Early Bird Days!

Time is ticking, with less than 60 days remaining until the excitement of Into the Box 2024 unfolds! Don't let this golden opportunity slip away; our exclusive Early Bird Pricing is here for a limited time only, available until March 31st. Why wait? Secure your seat now and take advantage of this steal!

Maria Jose Herrera
Maria Jose Herrera
March 20, 2024