Trying to understand the basics of routing

I apologize for the newbie question. I am looking into Coldbox for the first time. So far my experience with Commandbox has been amazing and now I want to start using a proper framework for development. MVC is new to me and I am trying to work out moving a site to using it. I am just failing at understanding what needs to be added to query records.

For example, on my site, I may have something like (hammer is the product and product id)

But then if i tried to do /products/tools/hammers/specialhammers/specialhammer-222333
this is where i get lost

Then on the same site, for example, i could have music, so then its


Am i looking at patterns, I just don’t understand how you set it up to query the right items in the db

No apologies for the “newbie” question, we’re all learning no matter how much experience we have. :slight_smile:

I am having trouble pinning down your exact question, though. Are you asking about routing (lots of talk about URL paths in your post) or are you asking about querying the database? Because how you query data doesn’t have anything to do with what the URL looks like.

Could you summarize into one question with a question mark at the end?

Like “Given that I have X and Y, how do I do A+B?”

Assuming this is more of a routing question…

Your first route would be something like this:

Which produces /products/tools/hammer-12345

You can also do optional placeholders for additional subcategories:

Which produces /products/tools/hammers/hammer-12345

I haven’t tried it, but that second one should work fairly well for your first two cases.

You’ll have trouble with more than a single optional placeholder, though. If you have a route of /products/:category/:subcategory?/:tag?/:productId and only one is included, how should ColdBox know which placeholder (:subcategory or :tag) that value goes in?

For this reason, I would stick to a few basic routes that work well and use query parameters for additional filters like ?tag=specialhammers or ?tag=top10.

Also, obligatory “Read the docs!”:

Yep you are the right path (meaning I need help with routing and not query) and I think I am there with you, but then on the Router.cfc, I am then not sure what I should be inputting for these. I am looking at the docs, Routing - ColdBox HMVC Documentation, I see routes for views, to handlers, etc

Would it be something like this?

route( "/products/:category/:subcategory?/:productId" )
        .toHandler( "products" );

Yep, that’s a good start. Take a look at the routing DSL for other route “modifiers”. i.e., to point to a specific method (called an “Action”) in that event handler:

Also, maybe it would make sense to split this up into two route definitions - one for listing products, the other for showing a single product page:

route( "/products/:category/:subcategory?/" )
        .withHandler( "Products" )
        .toAction( "list" );
route( "/products/:category/:subcategory?/:productId" )
        .withHandler( "Products" )
        .toAction( "details" );


  1. Remember to use a “terminator”, or the route will not register.
  2. Learn the difference between toHandler() and withHandler(). The former will tell ColdBox to stop building the route definition, whereas the latter only adds to the definition.

Hope this helps - you’re on the right track! :rocket:

Just want to say thank you, this helped me a lot and it worked out. It makes more sense to me now.

1 Like

Glad to hear it! Go ahead and mark as “Resolved” if that makes sense.