?s on building SES links w/ ColdBox 4.3

Hello all, I am having some troubles getting my SES URLs working properly. I am using ColdBox 4.3, and the issue is the same on Apache on my dev machine or IIS 8 on production, so evidently it is (as usual) something I am doing wrong.

Here is an example of a URL in its native format:

index.cfm?event=organizations.getOrganization&organizationID=#prc.organizations.organizationID#

When I configure basic SES URLs in my ColdBox app, and I use this:

#event.buildLink(linkTo=‘organizations.getOrganization’, queryString=‘organizationID=#prc.organizations.organizationID#’)#

I do get a nice pretty SES URL (example: http://127.0.0.1:62469/organizations/getOrganization/organizationID/376). Very nice!

But I also get a big fat “Variable ORGANIZATIONID is undefined” error from my handler, which is expecting a URL variable (organizationID). Hardcoding the organization ID, removing the octothorpes, etc, makes no difference. My handler just does not get the organization ID from trying to build the link this way.

This works: #event.buildLink(‘organizations.getOrganization’)#?organizationID=#prc.organizations.organizationID#

(It produces http://127.0.0.1:62469/organizations/getOrganization?organizationID=376)

And this works: #event.buildLink(‘organizations.getOrganization?organizationID=#prc.organizations.organizationID#’)#

(It produces the same URL).

The trouble is the query string in the URL, which I do not want. Since Example A does produce a nice friendly URL, my guess is that I am either not configuring the URL properly to pass along the variable to the handle, or I am doing something in the handler itself which causes it to not recognize the incoming variable. But I cannot figure out which!

Here is the specific line from handler referenced in the error message:

prc.getOrganization = organizationModel.getOrganization(organizationID);

I sure would appreciate any help! I am stuck. Many thanks!

If you’re trying to access the variable as “url. organizationID” then that is your issue. When using ColdBox, you should never hit form or URL directly. Instead, get it as “rc. organizationID”. The ColdBox framework takes care of combining URL, form, and if applicable-- Flash args into the request collection (rc) for you. This is for several reasons including

  • Encapsulation
  • Easier integration/unit testing
  • Using helper functions like event.getValue( ‘foo’, ‘defaultValue’ ) or event.paramValue()
  • programmatic access to all inputs to an event for operations like XSS cleaning
  • Your code doesn’t care where the variable came from

Thanks a lot Brad! I was not referencing the URL scope as such, but you were still right – in my handler, I was not referencing any scope at all! Once I changed

prc.getOrganization = organizationModel.getOrganization(organizationID);

to

prc.getOrganization = organizationModel.getOrganization(rc.organizationID);

all was well and I have a nice friendly URL! Many, many thanks. I am somewhat embarrassed to admit how much time I spent trying to figure this out before finally asking the experts!

No problem. In actuality, you were using the URL scope directly. When you used the variable with no scope prefix, ColdFusion was ‘scope hunting’ and form and url and two of the scopes it looks in by default. The SES URLs avoided the URL scope so the scope hunting couldn’t “find” the vars until you added the rc.

Thanks Brad, that’s good to know. You’ve made me realize that I need to go over a lot of my code — I now know for sure there are unscoped or improperly scoped variables in there!

Thanks also for your speedy and helpful answers. I really appreciate the explanation!