Friday, August 28, 2009

CF9 Front Controller Framework

I want to start writing a very simple front controller framework for CF9 that combines convention simplicity from ColdBox with configuration flexibility from Model-Glue, while borrowing a couple things from Rails/Grails/SpringMVC. Here's what a sample controller might look like:


component extends="Controller" {

public void function listUsers() {

var users = list("User");

render("users/list.cfm",{users=users});

}

public void function showUser() {

var user = get("User",params.userID);

render("users/show.cfm",{user=user});

}

public void function editUser() {

if(exists(flash,"user")) {
var user = flash.user;
}
else {
var user = get("User",params.userID);
}

render("users/edit.cfm",{user=user});

}

public void function saveUser() {

var user = get("User",params.userID);

populate(user,params);

var errors = validate(user);

if(!has(errors)) {

save(user);

flash.message = "User updated successfully";

redirect("showUser",{userID=user.getID()});

}
else {

flash.user = user;

flash.message = errors;

redirect("editUser",{userID=user.getID()});
}

}

public void function deleteUser() {

delete("User",params.userID);

flash.message = "User deleted successfully";

redirect("listUsers");

}

}

2 comments:

  1. I like the looks of this.

    Have you looked at FW/1? Looks a lot like it.

    ReplyDelete
  2. Yeah I've look at FW/1 a little bit and it does look pretty similar, but afterwhile all frameworks end up looking alike, with the main difference being the amount of syntactic sugar each one has.

    One thing about FW/1 that I wasn't such a huge fan of was it's reliance on setter-based dependency injection. I'd much rather use an annotation-based convention using the "beans" scope or cfproperty tags than see a slew of getters/setters at the bottom of my components. Here's from blog.cfc inside the FW/1 LitePost example:

    function setBookmarkService(bookmarkService) {
    variables.bookmarkService = bookmarkService;
    }

    function setCategoryService(categoryService) {
    variables.categoryService = categoryService;
    }

    function setCommentService(commentService) {
    variables.commentService = commentService;
    }

    function setEntryService(entryService) {
    variables.entryService = entryService;
    }

    function setRSSService(rssService) {
    variables.rssService = rssService;
    }

    function setSecurityService(securityService) {
    variables.securityService = securityService;
    }

    function setUserService(userService) {
    variables.userService = userService;
    }

    ReplyDelete

Note: Only a member of this blog may post a comment.