Linking Pages
CFWheels does some magic to help you link to other pages within your app. Read on to learn why you'll rarely use an <a> tag ever again.
CFWheels's built-in linkTo() function does all of the heavy lifting involved with linking the different pages of your application together. You'll generally be using linkTo() within your view code.
As you'll soon realize, the linkTo() function accepts a whole bunch of arguments. We won't go over all of them here, so don't forget to have a look at the documentation for the complete details.
Default Wildcard Linking
When installing CFWheels, if you open the file at app/config/routes.cfm
, you'll see something like this:
The call to wildcard() allows a simple linking structure where we can use the linkTo() helper to link to a combination of controller and action.
For example, if we had a widgets
controller with a new
action, we could link to it like this:
That would generally produce this HTML markup:
Linking to Routes
If you're developing a non-trivial CFWheels application, you'll quickly grow out of the wildcard-based routing. You'll likely need to link to URLs containing primary keys, URL-friendly slugged titles, and nested subfolders. Now would be a good time to take a deep dive into the Routing chapter and learn the concepts.
When you're using linkTo() to create links to routes, you need to pay attention to 2 pieces of information: the route name and any parameters that the route requires.
Let's work with a set of sample routes to practice creating links:
With this in place, we can load the webroot of our application and click the "Routes" link in the debugging footer to get a list of our routes. You'll see information presented similarly to this:
(As you become more experienced, you'll be able look at routes.cfm
and understand what the names and parameters are. Of course, this Routes functionality is a great tool too.)
If we want to link to the routes named newWidget
and widgets
, it's fairly simple:
As you can see, you create links by calling a method with the route name passed into the route
argument. That will generate these links:
The widget
route requires an extra step because it has that [key]
parameter in its pattern. You can pass that parameter into linkTo
as a named argument:
That will produce this markup:
If you have a route with multiple parameters, you must pass all of the placeholders as arguments:
Linking to Resources
Resources are the encouraged routing pattern in CFWheels, and you will likely find yourself using this type of route most often.
Once you setup a resource in app/config/routes.cfm
, the key is to inspect the routes generated and get a feel for the names and parameters that are expected.
Consider this sample posts
resource:
We would see these linkable routes generated related to the posts. (See the chapter on Form Helpers and Showing Errors for information about posting forms to the rest of the routes.)
If we wanted to link to the various pages within that resource, we may write something like this on the index:
The above code would generate markup like this:
A Deep Dive into Linking and Routing
The Routing chapter lists your options for generating URLs that are available in your application. Following is an explanation of how to link to the various types of routes available.
Namespaces
Namespaces will generally add the namespace name to the beginning of the route.
Consider this namespace:
To link to the roles
resource, you would prefix it with the namespace name:
However, new
and edit
routes add the action name to the beginning of the route name:
Nested Resources
You have the ability to nest a resource within a resource like so:
To link to the pages
resource, you add the parent resource's singular name first (e.g., the parent website
is added, making the route name websitePage
):
Linking to a Delete Action
CFWheels 2.0 introduced security improvements for actions that change data in your applications (i.e., creating, updating, and deleting database records). CFWheels protects these actions by requiring that they happen along with a form POST
in the browser.
A common UI pattern is to have a link to delete a record, usually in an admin area. Unfortunately, links can only trigger GET
requests, so we need to work around this.
To link to a delete request's required DELETE
method, we need to code the link up as a simple form with submit button:
The buttonTo() helper generates a form with submit button. As you can see from the example, you can style the submit button itself by prepending any arguments with input
(e.g., inputClass
).
Then it is up to you to style the form and submit button to look like a link or button using CSS (using whatever class
es that you prefer in your markup, of course).
If you need even more control, you can code up your own startFormTag() with whatever markup that you like. Just be sure to pass method="delete"
to the call to startFormTag
.
By the way, this will work with any request method that you please: post
, patch
, and put
as well as delete
.
Extreme Example
If we were to use all of the parameters for linkTo(), our code may look something like this:
Which would generate this HTML (or something like it):
Images and Other Embedded HTML in Link Texts
If you'd like to use an image as a link to another page, pass the output of imageTag() to the text
argument of linkTo()and use the encode
argument to instruct linkTo
to only encode attributes
:
You can also use your CFML engine's built-in string interpolation to embed other HTML into the link text in a fairly readable manner:
Security Notice
If you decide to opt out of encoding, be careful. Any dynamic data passed in to un-encoded values should be escaped manually using your CFML engine's EncodeForHtml()
function.
Adding Additional Attributes Like class, rel, and id
Like many of the other CFWheels view helpers, any additional arguments that you pass to linkTo() will be added to the generated <a>
tag as attributes.
For example, if you'd like to add a class
attribute value of button to your link, here's what the call to linkTo() would look like:
The same goes for any other argument that you pass, including but not limited to id, rel, onclick
, etc.
What If I Don't Have URL Rewriting Enabled?
CFWheels will handle linking to pages without URL rewriting for you automatically. Let's pretend that you still have CFWheels installed in your site root, but you do not have URL rewriting on. How you write your linkTo() call will not change:
CFWheels will still correctly build the link markup:
Linking in a Subfolder Deployment of CFWheels
The same would be true if you had CFWheels installed in a subfolder, thus perhaps eliminating your ability to use URL Rewriting (depending on what web server you have). The same linkTo() code above may generate this HTML if you had CFWheels installed in a subfolder called foo
:
Use the linkTo() Function for Portability
An <a>
tag is easy enough, isn't it? Why would we need to use a function to do this mundane task? It turns out that there are some advantages. Here's the deal.
CFWheels gives you a good amount of structure for your applications. With this, instead of thinking of URLs in the "old way," we think in terms of what route we're sending the user to.
What's more, CFWheels is smart enough to build URLs for you. And it'll do this for you based on your situation with URL rewriting. Are you using URL rewriting in your app? Great. CFWheels will build your URLs accordingly. Not fortunate enough to have URL rewriting capabilities in your development or production environments? That's fine too because CFWheels will handle that automatically. Are you using CFWheels in a subfolder on your site, thus eliminating your ability to use URL rewriting? CFWheels handles that for you too.
If you see the pattern, this gives your application a good deal of portability. For example, you could later enable URL rewriting or move your application to a different subfolder. As long as you're using linkTo() to build your links, you won't need to change anything extra to your code in order to accommodate this change.
Lastly, if you later install a plugin that needs to modify link markup, that plugin's hook is the linkTo() helper.
Oh, and another reason is that it's just plain cool too. ;)
Last updated