[ColdBox 3.7] routing and handler naming.

I would like to have a naming convention for my handlers so that they always end in “Controller” but I would like for the urls used to access the handler to only have to include the unique part of the handlers name.

For example /app/search/runSearch would execute the runSearch function in searchController.cfc.

Is there a way to accomplish this using ses routing rules?

Thanks

Hi Ryan,

Yes its possible, you can configure your routes in config/Routes.cfm
addRoute(pattern="/app/search/runSearch", handler=“searchController”, action=“runSearch”);

Or a bit more generic, would be:

addRoute(pattern="/app/search/:action?",handler=“searchController”);

The :action would be determined at run time so any method in the controller would work. That way you only need one route per controller.

Luis, what do you think about a “routeAlias” annotation on hanlder CFCs to specify additional routes that map to that handler?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Also, a follow up on this-- what really is the benifit of having the word “controller” on all your handlers? Is it enough that they are all already in a folder called “handlers”. I used to start all my handlers with “eh” and all my views with “vw”, but eventually realized it was just wasted typing. The folder the file lived in told me all I needed to know about it.

If it makes you feel better, you can rename the default handler convention so they’re all in a folder called “controllers”. Just put this in your config CFC.

conventions = {
	handlersLocation = "controllers"
};

Gotta’ love ColdBox :slight_smile:

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Can you expand on the route alias? As annotations?

Thanks for all your suggestions, I was hoping for a generic way to map all the handlers in this way so a new mapping is not required for each handler.

Brad, my reasoning for wanting to name the handlers in this way is to make them easier to identify in eclipse. Often I have many files open and I find it beneficial to be able to locate the controller at a glace, I find a generic name like Search.cfm doesn’t stand out. It also allows me to use the file search to quickly locate all the controllers in the application. It’s just a personal preference and not a huge deal so without a generic solution, I’ll probably just leave things as is so the other developers on our team don’t need to add mappings for every controller they create.

Luis, I already have them in a folder called “controllers” and that does help to some extent, so thanks for your suggestion.

Cheers!

Well, at first I was thinking something like this:

myWackyHandler.cfc
component routeAlias=“wacky,wackster” {
}

But after thinking, it may be better along with the other THIS settings we let people set in the CFC

myWackyHandler.cfc
component {

this.prehandler_only = “”;
this.prehandler_except = “”;
this.posthandler_only = “”;
this.posthandler_except = “”;
this.aroundHandler_only = “”;
this.aroundHandler_except = “”;
this.allowedMethods = {};
this.routeAlias=“wacky,wackster”;

}

Maybe the name shouldn’t have the word “route” in it since that’s implies SES urls. The following URLs would all use the CFC above

Non-SES
site.com/index.cfm?event=myWackyHandler.index
site.com/index.cfm?event=wacky.index
site.com/index.cfm?event=wackster.index

SES
site.com/index.cfm/myWackyHandler/index
site.com/index.cfm/wacky/index
site.com/index.cfm/wackster/index

Perhaps handlerAlias. I don’t think it should just be “alias” since it might be confused with the component annotation “alias” that WireBox uses.

Of course, this still assumes that the package wouldn’t change, meaning if myWackyHandler.cfc was in a folder called “foobar”, the URLs would still be this:

site.com/index.cfm/foobar/myWackyHandler/index
site.com/index.cfm/foobar/wacky/index
site.com/index.cfm/foobar/wackster/index

In addition to a simple alias to the handler, would it be useful to have an “entrypoint” setting as well which would be any route that completely matched the package and handler?

Then the following:

foobar/myWackyHandler.cfc
component {
this.entrypoint=“foowack”;
}

Would be accessible from here:

site.com/index.cfm/foowack

Basically the same as entering:

addRoute(pattern="/foowack?",handler=“foobar/myWackyHandler”);

Thoughts?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Yes,

This is interesting. To tell you the truth I always stayed away from
putting metadata about the request in the handlers and always decoupled it
to the routes file, so you have only one place to look for routes. I am
still divided as I always thought of maybe adding not only entry points for
handlers but also for actions themselves like this

/**
* @route GET:/users/
*/
function list(event,rc,prc){

}

I am not sure, maybe we can set this to a vote.

Luis F. Majano
CEO
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com
Social: twitter.com/lmajano facebook.com/lmajano

Hi Luis,

My vote for having annotation based Route. Just like we do in Spring3 [ @RequestMapping("/editPet.do") ] … :slight_smile:

I’m a little torn because I inclined to agree with you that separating routing from controller logic is a positive. However, I’ve also worked with Spring a fair bit and I feel that the convenience of annotation based routes outweigh the negatives.

Just my two cents.

Can somebody create a nice Ticket on Jira, describing this update, maybe something to add for ColdBox 4.

signature0.jpg

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

Created:
https://ortussolutions.atlassian.net/browse/COLDBOX-210
https://ortussolutions.atlassian.net/browse/COLDBOX-211

I put suggested implementations in the ticket descriptions. Feel free to debate the correct way in the comments :slight_smile:

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com