{"__v":1,"_id":"5509797daa9bd525001a0656","category":{"__v":16,"_id":"550974cc368a56170041475b","project":"55070e814bb83b2500ec9404","version":"550974cb368a561700414757","pages":["55097808ad1f0523008ecbca","550979624c7c3f2300aabf01","5509797daa9bd525001a0656","550979a0aa9bd525001a0658","550979ba4c7c3f2300aabf03","550979e6dd77250d007369f2","55097a02dd77250d007369f4","55097a0fad1f0523008ecbce","55097a1baa9bd525001a065a","55097a212dd6a11900e6e7b0","55097a28ad1f0523008ecbd0","55097a304c7c3f2300aabf05","55097a3add77250d007369f6","55097a402dd6a11900e6e7b2","55097a6fdd77250d007369f8","55097a77ad1f0523008ecbd2"],"reference":false,"createdAt":"2015-03-18T11:08:22.079Z","from_sync":false,"order":2,"slug":"handling-requests-with-controllers","title":"Handling Requests with Controllers"},"project":"55070e814bb83b2500ec9404","user":"55070d24d30b3f190011b941","version":{"__v":1,"_id":"550974cb368a561700414757","forked_from":"55070e814bb83b2500ec9407","project":"55070e814bb83b2500ec9404","createdAt":"2015-03-18T12:51:23.709Z","releaseDate":"2015-03-18T12:51:23.709Z","categories":["550974cc368a561700414758","550974cc368a561700414759","550974cc368a56170041475a","550974cc368a56170041475b","550974cc368a56170041475c","550974cc368a56170041475d","550974cc368a56170041475e"],"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.4.0","version":"1.4"},"updates":[],"createdAt":"2015-03-18T13:11:25.808Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"order":2,"body":"When a user submits a form, you do not want to show any content on the page that\nhandles the form submission! Why? Because if you do, and the user hits refresh \nin their browser, the form handling code could be triggered again, possibly \ncausing duplicate entries in your database, multiple emails being sent, etc.\n\n## Remember to Redirect\n\nTo avoid the above problem, it is recommended to always redirect the user after \nsubmitting a form. In Wheels this is done with the [redirectTo()][1] function. \nIt is basically a wrapper around the `cflocation` tag in CFML.\n\nBeing that [redirectTo()][1] is a Wheels function, it can accept the \n`route, controller, action`, and `key` arguments so that you can easily redirect\nto other actions in your application.\n\n## Three Ways to Redirect\n\nLet's look at the three ways you can redirect in Wheels.\n\n### 1. Redirecting to Another Action\n\nYou can redirect the user to another action in your application simply by \npassing in the `controller, action`, and `key` arguments. You can also pass in \nany other arguments that are accepted by the [URLFor()][2] function, like host, \nparams, etc. (The [URLFor()][2] function is what Wheels uses internally to \nproduce the URL to redirect to.)\n\n### 2. Redirection Using Routes\n\nIf you have configured any routes in `config/routes.cfm`, you can use them when \nredirecting as well. Just pass in the route's name to the route argument \ntogether with any additional arguments needed for the route in question. You can\nread more about routing in the [Using Routes][3] chapter.\n\n### 3. Redirecting to the Referring URL\n\nIt's very common that all you want to do when a user submits a form is send them\nback to where they came from. (Think of a user posting a comment on a blog post \nand then being redirected back to view the post with their new comment visible \nas well.) For this, we have the `back` argument. Simply pass in `back=true` to \n[redirectTo()][1], and the user will be redirected back to the page they came \nfrom.\n\n### Handling an Invalid Referrer\n\nThe referring URL is retrieved from the `cgi.http_referer` value. If this value \nis blank or comes from a different domain than the current one, Wheels will \nredirect the visitor to the root of your website instead.\n\nIf you want to specify exactly where to send the visitor when the referring \ndomain is blank/foreign, you can pass in the normal [URLFor()][2] arguments like\n`route, controller, action`, etc. These will be used only when Wheels can't \nredirect to the referrer because it's invalid.\n\n## Appending Params\n\nSometimes it's useful to be able to send the visitor back to the same URL they \ncame from but with extra parameters added to it. You can do this by using the \n`params` argument. Note that Wheels will append to the URL and not replace it in\nthis case.\n\n## `addToken` and `statusCode` Arguments\n\n[redirectTo()][1] uses `<cflocation>` under the hood; if you need to pass client\nvariable information automatically in the URL for client management purposes, \nsimply set `addToken=true`. \n\nYou can also set the type of redirect to something other than the default `302` \nredirect, by passing in `statusCode=3xx`. `301` indicates a permanent redirect.\n\n[1]: doc:redirectto\n[2]: doc:urlfor\n[3]: doc:using-routes","excerpt":"Use redirection to keep your application user friendly.","slug":"redirecting-users","type":"basic","title":"Redirecting Users"}

Redirecting Users

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. ## Remember to Redirect To avoid the above problem, it is recommended to always redirect the user after submitting a form. In Wheels this is done with the [redirectTo()][1] function. It is basically a wrapper around the `cflocation` tag in CFML. Being that [redirectTo()][1] is a Wheels function, it can accept the `route, controller, action`, and `key` arguments so that you can easily redirect to other actions in your application. ## Three Ways to Redirect Let's look at the three ways you can redirect in Wheels. ### 1. Redirecting to Another Action You can redirect the user to another action in your application simply by passing in the `controller, action`, and `key` arguments. You can also pass in any other arguments that are accepted by the [URLFor()][2] function, like host, params, etc. (The [URLFor()][2] function is what Wheels uses internally to produce the URL to redirect to.) ### 2. Redirection Using Routes 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 [Using Routes][3] chapter. ### 3. Redirecting to the Referring URL 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 `back` argument. Simply pass in `back=true` to [redirectTo()][1], and the user will be redirected back to the page they came from. ### Handling an Invalid Referrer The referring URL is retrieved from the `cgi.http_referer` value. If this value is blank or comes from a different domain than the current one, Wheels 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()][2] arguments like `route, controller, action`, etc. These will be used only when Wheels can't redirect to the referrer because it's invalid. ## Appending Params 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 `params` argument. Note that Wheels will append to the URL and not replace it in this case. ## `addToken` and `statusCode` Arguments [redirectTo()][1] uses `<cflocation>` under the hood; if you need to pass client variable information automatically in the URL for client management purposes, simply set `addToken=true`. You can also set the type of redirect to something other than the default `302` redirect, by passing in `statusCode=3xx`. `301` indicates a permanent redirect. [1]: doc:redirectto [2]: doc:urlfor [3]: doc:using-routes