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

Adobe CFSummit 2024 Recap | Ortus Solutions

Adobe CFSummit 2024 Recap | Ortus Solutions

A huge thank you to everyone who visited our booth! We had the pleasure of engaging in meaningful conversations about Modern CFML and ColdFusion development, sharing insights, and exploring exciting new ideas. Visitors also got a chance to participate in some fun activities and win fabulous prizes, including our customized t-shirts, which were a huge hit!

Maria Jose Herrera
Maria Jose Herrera
October 02, 2024
BoxLang September Newsletter

BoxLang September Newsletter

Welcome to the September edition of the BoxLang Newsletter! This month has been packed with exciting updates, new features, and enhancements that continue to shape BoxLang into a robust and versatile programming language. In this edition, we highlight the latest beta releases, new integrations, and key features that have been introduced.

Maria Jose Herrera
Maria Jose Herrera
October 01, 2024
Fall Savings: Unlock Exclusive Discounts Until October 31st!

Fall Savings: Unlock Exclusive Discounts Until October 31st!

Get ready for the Ortus October Fall Savings! This is your opportunity to elevate your development experience with fantastic offers designed for everyone—from seasoned developers to tech enthusiasts. Our special promotions are here to help you achieve your goals while saving money!

Don’t Miss Out on These Amazing Offers!

Use the code FALLSAVINGS24 at checkout to enjoy these incredible discounts before they expire on October 31st

Cristobal Escobar
Cristobal Escobar
October 01, 2024