Creating a Model

Background

Models contain the majority of the code and processing in a Coldbox Application. They are typically located in the models folder and are, for the most part, regular CF code.

Creating a Model

  1. In the Models folder, create a CFC named userService.cfc with a function called allUsers.

component {
    function allUsers(){
        return queryExecute("select * from users");
    }
}

Now we need to tie the handler to the model.

Adapt the handler to this:

component extends="Coldbox.system.RestHandler" {
    property name="userService" inject="models.userService";

    function index(event, rc, prc){
        var allUsers = userService.allUsers();
        prc.response.setData(allusers);
    }


}

Now, the UserService model exists as a property in the handler which calls the allusers() method and returns the result to the caller. Reinit the app and put https://justenough.local/users into the companion app on the Coldbox --> Getting Started page.

Last updated