[coldbox-3.8.1] Routes and buildLink() question

Looking at the following route definition:

addRoute(pattern=’/blog/:entry’, handler=‘blog’, action=‘showEntry’);

And these two example links in my HTML:

View this entry
View this entry

I find myself in this “moral dilemma”. By building the first link, I am bound to SES. I couldn’t turn it off if I wanted w/o refactoring every instance of buildLink to the second style.

The second buildLink() style, using the linkTo and queryString parameters, however, doesn’t really work. The link comes out as “blog/entry/myLink”, so that’s not good.

I would have almost thought that using buildLink() with the specific handler (blog) and parameters (:entry), that the buildLink() would be reading my routes and understand the correct way to build that link.

Am I over thinking buildLink()'s capabilities? Or do I need to check for SESMod() everytime I write out a link and use a separate style depending on the availability of SES?

If you aren’t going to use SES, then maybe you should also remove the SES interceptor from your config, this can be found in the Coldbox.cfc config file. I also believe the translate attribute plays a part in that role as well, not sure never run into this issue myself.

I believe the translate oly works for using forward slash or dot notation in the building, but if you want the correct method in the query then you would not pass the query like this.

For example

View this entry

It might seem old school, but that the buildLink is mainly to translate the links to SES URL’s. if the SES Interceptor is enabled the it is automatically assuming it is SES… At least that is how I recall it working.

Forgot to add, all this is explained here.

http://wiki.coldbox.org/wiki/URLMappings.cfm

Andrew, I pored over that documentation to understand what I was seeking.

However, your example of what I should not do…that wasn’t one of my examples. There’s no ? in my two building code examples.

Unfortunately, this also didn’t address my question. It seems that I must KNOW (anywhere I put a link to a blog post), what the format is that I have defined in the route. The buildLink() function isn’t able to interpret the fact that I need to “build a link to a blog entry”. If I needed to move the blog entry point to a different format, like ‘/blog/view/:entry’, then all the places I built those links would require refactoring. The buildLink() function doesn’t see that there’s only one variable in the URL style, that I’m needing to link to “I wanna see a blog post, make this stuff work”.

I can see, though, that a cfc being built that can interpret “what you want to do” in conjunction with the routes to return whatever today’s route style is for a given combination would be useful.

I might try to come up with something like that.

No, I think you misunderstood. If you follow that page link right down to the bottom, you will see the following attached below. It clearly states that it builds SES URL’s for you, in other words what it does is translate it too something like this, using your example.

/blog/index.cfm?entry=permalink

and your second example will be

/blog/entry/permalink

What you want is your first option, because the second example you gave is for SES only. So as I explained, is that you need to do this, repeat from my last post.

View this entry

Which will result in the first example I gave above, but will repeat here.

/blog/index.cfm?entry=permalink

I assume the permalink is a string that contains a permalink,

HTH

event.buildLink()

public any buildLink(string linkto, [boolean translate='true'], [boolean ssl='false'], [string baseURL=''], [string queryString=''])

The request context has a method called buildLink() that will build SES URLs for you. Just pass in the route or event and it will create the appropriate URL for you:

**<a** href="#event.buildLink('home.about')#">About**</a>**
**<a** href="#event.buildLink('user.edit.id.#user.getID()#')#">Edit User**</a>**

That pasting should have contained this as well.

The buildlink will translate any periods (.) to (/) slashes for you automatically

> The link comes out as “blog/entry/myLink”, so that’s not good.

Why isn’t that good? Do you say that just because you were hoping to not have the “/entry/” part in there?

> I would have almost thought that using buildLink() with the specific handler (blog) and parameters (:entry), that the buildLink() would be reading my routes and understand the correct way to build that link.

Nope. Build link doesn’t know anything about your routes. It simply takes an even and an optional query string and makes a URL out of it that either uses SES or doesn’t.

> Am I over thinking buildLink()'s capabilities?

Yep.

Or do I need to check for SESMod() everytime I write out a link and use a separate style depending on the availability of SES?

Unless you’re writing a module or library for people to install in their own app, is there a reason why you would ever need to turn SES on or off? If you are configuring routes, you’re kind of signing up for SES right there. I’d just stick with that decision and run with it. BuildLink() can make either style, but is restricted to passing ad-hoc variables in the /name/value format.

> It seems that I must KNOW (anywhere I put a link to a blog post), what the format is that I have defined in the route.

Yep, that’s correct. If you dream up a route of your choosing, your code has to be “aware” of its format. There’s nothing wrong with this. ContentBox actually many perfect examples of this. The CBHelper plugin has a handful of helper method for the specific purpose of building links to a specific entity.

Ex:
https://github.com/Ortus-Solutions/ContentBox/blob/master/modules/contentbox/plugins/CBHelper.cfc#L487

So there’s nothing wrong with creating a utility in your app that builds a link to a blog for you.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

What I had been hoping for was that I didn’t need the “/entry” part. That this would come from the buildLink() somehow.

It’s not a huge problem, just one where I was misunderstanding the relationship between buildLink() and the routes.

Thanks guys!

Well you can, but you need something like mod_rewrite or ISAP_rewrite or something to rewrite the incoming URL to what your routes require.

So if you know it is entry, for blog you could just have

/blog/permalink

But the route would see this, once converted for ColdFuson, would be

mydomain.com/index.cfm?event=blog&entry=permalink

provided that the route is

addRoute(pattern=’/blog/:entry’, handler=‘blog’, action=‘showEntry’);