Skip to content

Instantly share code, notes, and snippets.

@neokoenig
Created February 10, 2015 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neokoenig/f71cb13a63844861f152 to your computer and use it in GitHub Desktop.
Save neokoenig/f71cb13a63844861f152 to your computer and use it in GitHub Desktop.
Simple cfwheels route example
/**
* Example controller: add these routes: The order is important in how they get matched. Top down!
*
* // Example Static Route - it's above the user bit so will get precedence
* addRoute(name="about", pattern="/about", controller="pages", action="about");
*
* // Here's our user routes: from this point on, anything like "/foo/" will get interpreted as username = foo etc.
* // These two routes basically mimic wheels defaults, just with the username prefix
* addRoute(name="user", pattern="/[username]/[controller]/[action]/[key]");
* addRoute(name="user", pattern="/[username]/[controller]/[action]/");
*
* // We can also add overrides for certain static routes
* addRoute(name="user", pattern="/[username]/contact", controller="default", action="contact");
* addRoute(name="user", pattern="/[username]/foo", controller="default", action="foo");
*
* // But if we just have /john/dostuff, we just want the index of the dostuff controller
* addRoute(name="user", pattern="/[username]/[controller]/", action="index");
* addRoute(name="user", pattern="/[username]/", controller="default", action="index");
*
* // Home page
* addRoute(name="home", pattern="", controller="pages", action="home");
*
* **/
component extends="Controller" hint="Routes example"
{
/**
* @hint Constructor.
*/
public void function init() {
filters(through="f_getUser");
}
/******************** Public***********************/
/**
* @hint Will appear at /john/
*/
public void function index() {
// Because of our filter, we should already have the user object here.
}
/**
* @hint will appear at /john/contact/: this is also accessible via /john/default/contact/
*/
public void function contact() {
// Because of our filter, we should already have the user object here.
}
/**
* @hint will appear at /john/foo/: this is also accessible via /john/default/foo/
*/
public void function foo() {
// Because of our filter, we should already have the user object here.
}
/******************** Private *********************/
/**
* @hint Get the user: this is optional, i'm assuming you'd want some user data for each request
*/
private void function f_getUser() {
if(structKeyExists(params, "username") AND len(params, "username") GT 1){
user=model("user").findOne(where="username = '#params.username#'");
if(isObject(user)){
// All good, we've got our user
} else {
// Hmm, no user found, redirect away
redirectTo(route="home", error="Sorry, can't find that user");
}
} else {
redirectTo(route="home", error="Sorry, can't find that user");
}
}
/******************** Ajax/Remote/Misc*************/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment