////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////


== createLink



=== Purpose


Creates a link that can be used where necessary (for example in an href, JavaScript, Ajax call etc.)


=== Examples


Example controller for an application called "shop":

[source,groovy]
----
class BookController {

    static defaultAction = "list"

    def list() {
        [books: Book.list(params)]
    }

    def show() {
        [book: Book.get(params.id)]
    }
}
----

Example usages for above controller:

[,xml]
----
<!-- generates "/shop/book/show/1" -->
<g:createLink action="show" id="1" />

<!-- generates "/shop/book/show?foo=bar&boo=far" -->
<g:createLink action="show" params="[foo: 'bar', boo: 'far']"/>

<!-- generates "/shop/book" -->
<g:createLink controller="book" />

<!-- generates "/shop/book/list" -->
<g:createLink controller="book" action="list" />

<!-- generates "/shop/book/list" -->
<g:createLink url="[action:'list',controller:'book']" />

<!-- generates a link tot he book controller in the publishing namespace -->
<g:createLink controller="book" namespace="publishing"/>

<!-- generates "https://portal.mygreatsite.com/book" -->
<g:createLink controller="book" absolute="true"/>

<!-- generates "https://admin.mygreatsite.com/book" -->
<g:createLink controller="book" base="https://admin.mygreatsite.com"/>

<!-- generates "/shop/book/list/1?title=The+Shining&author=Stephen+King" -->
<g:createLink controller="book" action="list"
              params="[title: 'The Shining', author: 'Stephen King', id: '1']"/>
----

Example as a method call in GSP:

[,xml]
----
<a href="${createLink(action: 'list')}">my link</a>
----

results in:

[source,xml]
----
<a href="/shop/book/list">my link</a>
----


=== Description


Attributes

* `action` (optional) - The name of the action to use in the link; if not specified the default action will be linked
* `controller` (optional) - The name of the controller to use in the link; if not specified the current controller will be linked
* `namespace` (optional) - The namespace of the controller to use in the link
* `plugin` (optional) - The name of the plugin which provides the controller
* `id` (optional) - The id to use in the link
* `fragment` (optional) - The link fragment (often called anchor tag) to use
* `mapping` (optional) - The link:{guidePath}/theWebLayer.html#namedMappings[named URL mapping] to use to rewrite the link
* `method` (optional) - The HTTP method specified in the corresponding URL mapping
* `params` (optional) - A map containing URL query parameters for the link
* `url` (optional) - A Map containing the action, controller, id etc.
* `uri` (optional) - A string for a relative path in the running app.
* `relativeUri` (optional) - Used to specify a uri relative to the current path.
* `absolute` (optional) - If `true` will prefix the link target address with the value of the `grails.serverURL` property from the application configuration, or http://localhost:8080 if there is no setting in the config and not running in production.
* `base` (optional) - Sets the prefix to be added to the link target address, typically an absolute server URL. This overrides the behaviour of the `absolute` property if both are specified.
* `event` (optional) - The name of a Webflow event to trigger for the flow associated with the given `action`. Requires the Webflow plugin.

