Using Multi-Word-Actions in ColdBox 2.6

Posted on November 11, 2008 at 5:06 PM in ColdBox, ColdFusion

The other day Jason Durham posed a question to me:

How can I go about using multi-word actions, using hyphens as separators, in my ColdBox handlers?

After my initial "Why on earth would you want to do that?" response, Jason pointed out the SEO benefits of doing so, which makes perfect sense. (That's when I thought to myself, "Hmmm, why didn't I think of that?")

A ColdBox URL looks like this:

Non-SES URL

  1. http://www.mydomain.com/index.cfm?event=handler.action

SES URL

  1. http://www.mydomain.com/index.cfm/handler/action

Typically speaking, when the action is multiple words, say 'list products', one would simply camel-case it, and it would become 'listProducts'. Jason's idea was to separate the words with a hyphen (-), giving search engines something more meaningful to index. So 'list products' would become 'list-products'. ColdFusion does not allow hyphens in method names, but thankfully, ColdBox allows for a ridiculously simple solution to the problem.

onMissingAction()

New in ColdBox 2.6 is onMissingAction(). Much like onMissingMethod() in CF8, onMissingAction() is an action that you can place into your handlers that will fire when an invalid action is requested. So let's take a look at how to use onMissingAction() to implement our need for multi-word actions.

  1. <cffunction name="onMissingAction" hint="I am fired when an action is called on me that doesn't exist" returntype="void" output="no" access="public">
  2. <cfargument name="Event" hint="The event object" required="yes" type="coldbox.system.beans.requestContext" />
  3. <cfargument name="missingAction" hint="The name of the requested action" required="true" type="string" />
  4. <cfscript>
  5. var theRealAction = replace(missingAction, "-", "", "all");
  6. if ( structKeyExists(this, theRealAction) ) {
  7. runEvent( event.getCurrentHandler() & "." & theRealAction );
  8. } else {
  9. runEvent( replace(getSetting("onInvalidEvent"), "/", ".", "all") );
  10. }
  11. </cfscript>
  12. </cffunction>

So what does this little snippet do? It's pretty straight forward:

  1. The invalid action is captured by onMissingAction() [Line 1]
  2. We create a variable named 'theRealAction' by stripping all hyphens from the captured invalid action [Line 6]
  3. We check to see if the handler has a method named the same as our 'theRealAction' variable [Line 8]

    1. If yes, we run the event [Line 9]
    2. If no, we grab the onInvalidEvent setting and run that event [Line 11]

One note to make about the onInvalidEvent setting. If you are using SES URLs in ColdBox, this setting needs to use a slash (/) instead of a dot (.). Knowing this, in Step 3.2 we're replacing slashes with dots, since the runEvent() method requires the dot.

With our onMissingAction() action now in place, when the browser requests our list-products URL...

  1. http://www.mydomain.com/index.cfm/catalog/list-products

... the catalog.listProducts() event is the one that ultimately fires.

Happy SEO with your ColdBox-powered SES URLs. :-)

Comments
(Comment Moderation is enabled. Your comment will not appear until approved.)

On 11/11/08 at 6:12 PM, Jason Durham said:

Thanks for your help Matt!

On 11/11/08 at 8:27 PM, Sana said:

@Matt great stuff, by the way coldbox routing system allow you map your URL to internal handler-method like this 'catalog/list-products -->catalog.ListProduct'
Again your solution is great as well.

On 11/11/08 at 8:30 PM, Matt Quackenbush said:

@ Sana- You are absolutely correct, and that was my first suggestion, but when I thought about it for a moment, I liked the onMissingAction() solution much better, since it is completely dynamic. If you map it via routes, then it would become cumbersome always having to modify your routes file. ;-)

On 11/11/08 at 9:05 PM, Sana said:

@Matt I forget to mention that why I like to have external URL seperate from internal names. As I am big fan of having more OO naming convention for all internal class /methods name, external URL could be different in future to meet the needs of SEO or etc. But internal system naming convention are more important for better understanding and the purpose of class/methods and implementation.

Its just a opinion and also I am happy with anything which works :)
CodeBassRadio

Latest Articles

Eventually something really brilliant and witty will appear right here.

Calendar

April 2024
S M T W T F S
« Mar  
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        

Subscribe

Enter a valid email address.

The Obligatory Wish List