Use redirection to keep your application user friendly.
When a user submits a form, you do not want to show any content on the page that handles the form submission! Why? Because if you do, and the user hits refresh in their browser, the form handling code could be triggered again, possibly causing duplicate entries in your database, multiple emails being sent, etc.
To avoid the above problem, it is recommended to always redirect the user after submitting a form. In CFWheels this is done with the redirectTo() function. It is basically a wrapper around the
cflocationtag in CFML.
Let's look at the three ways you can redirect in CFWheels.
You can redirect the user to another action in your application simply by passing in the
controller, action, and
keyarguments. You can also pass in any other arguments that are accepted by the URLFor() function, like host, params, etc. (The URLFor() function is what CFWheels uses internally to produce the URL to redirect to.)
If you have configured any routes in
config/routes.cfm, you can use them when redirecting as well. Just pass in the route's name to the route argument together with any additional arguments needed for the route in question. You can read more about routing in the Routing chapter.
It's very common that all you want to do when a user submits a form is send them back to where they came from. (Think of a user posting a comment on a blog post and then being redirected back to view the post with their new comment visible as well.) For this, we have the
backargument. Simply pass in
back=trueto redirectTo(), and the user will be redirected back to the page they came from.
The referring URL is retrieved from the
cgi.http_referervalue. If this value is blank or comes from a different domain than the current one, CFWheels will redirect the visitor to the root of your website instead.
If you want to specify exactly where to send the visitor when the referring domain is blank/foreign, you can pass in the normal URLFor() arguments like
route, controller, action, etc. These will be used only when CFWheels can't redirect to the referrer because it's invalid.
Sometimes it's useful to be able to send the visitor back to the same URL they came from but with extra parameters added to it. You can do this by using the
paramsargument. Note that CFWheels will append to the URL and not replace it in this case.
You can also set the type of redirect to something other than the default
302redirect, by passing in
statusCode=3xx. For example,
301indicates a permanent redirect.