View Helpers
linkTo()
Signature
Section titled “Signature”linkTo() — returns string
Available in: controller
Category: Link Functions
Description
Section titled “Description”Creates a link to another page in your application. Pass in the name of a route to use your configured routes or a controller/action/key combination. Note: Pass any additional arguments like class, rel, and id, and the generated tag will also include those values as HTML attributes.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text | string | no | — | The text content of the link. |
route | string | no | — | Name of a route that you have configured in config/routes.cfm. |
controller | string | no | — | Name of the controller to include in the URL. |
action | string | no | — | Name of the action to include in the URL. |
key | any | no | — | Key(s) to include in the URL. |
params | string | no | — | Any additional parameters to be set in the query string (example: wheels=cool&x=y). Please note that CFWheels uses the & and = characters to split the parameters and encode them properly for you. However, if you need to pass in & or = as part of the value, then you need to encode them (and only them), example: a=cats%26dogs%3Dtrouble!&b=1. |
anchor | string | no | — | Sets an anchor name to be appended to the path. |
onlyPath | boolean | no | true | If true, returns only the relative URL (no protocol, host name or port). |
host | string | no | — | Set this to override the current host. |
protocol | string | no | — | Set this to override the current protocol. |
port | numeric | no | 0 | Set this to override the current port number. |
href | string | no | — | Pass a link to an external site here if you want to bypass the CFWheels routing system altogether and link to an external URL. |
encode | any | no | true | When set to true, encodes tag content, attribute values, and URLs so that Cross Site Scripting (XSS) attacks can be prevented. Set to attributes to only encode attribute values and not tag content. |
Examples
Section titled “Examples”#linkTo(text="Log Out", controller="account", action="logout")# <!--- Ouputs: <a href="/account/logout">Log Out</a> ---> <!--- If you're already in the `account` controller, CFWheels will assume that's where you want the link to point ---> #linkTo(text="Log Out", action="logout")# <!--- Ouputs: <a href="/account/logout">Log Out</a> ---> #linkTo(text="View Post", controller="blog", action="post", key=99)# <!--- Ouputs: <a href="/blog/post/99">View Post</a> ---> #linkTo(text="View Settings", action="settings", params="show=all&sort=asc")# <!--- Ouputs: <a href="/account/settings?show=all&amp;sort=asc">View Settings</a> ---> <!--- Given that a `userProfile` route has been configured in `config/routes.cfm` ---> #linkTo(text="Joe's Profile", route="userProfile", userName="joe")# <!--- Ouputs: <a href="/user/joe">Joe's Profile</a> ---> <!--- Link to an external website ---> #linkTo(text="ColdFusion Framework", href="http://cfwheels.org/")# <!--- Ouputs: <a href="http://cfwheels.org/">ColdFusion Framework</a> ---> <!--- Give the link `class` and `id` attributes ---> #linkTo(text="Delete Post", action="delete", key=99, class="delete", id="delete-99")# <!--- Ouputs: <a class="delete" href="/blog/delete/99" id="delete-99">Delete Post</a> --->