# dispatch

Source: https://zotonic.com/docs/2437/dispatch

URI: https://zotonic.com/id/2437

Language: en



Dispatch or generate URLs or page paths. Useful to check dispatch rules or for client side code to dispatch page paths.

Dispatching URLs
----------------

The `path` returns the matched dispatch rule, controller, controller options, language, bindings and other dispatch information.

If no dispatch rule matches then 404 Not Found is returned.

Example:

```bash
curl -k 'https://example.test:8443/api/model/dispatch/get/path/nl/page/1234/hello'
```

Returns:

```json
{
    "result": {
        "bindings": {
            "id": "1234",
            "z_language": "nl",
            "zotonic_site": "example",
            "zotonic_dispatch_path": [
                "page",
                "1234",
                "hello"
            ],
            "zotonic_dispatch": "page",
            "slug": "hello"
        },
        "controller": "controller_page",
        "site": "example",
        "language": "nl",
        "dispatch_rule": "page",
        "path_tokens": [
            "page",
            "1234",
            "hello"
        ],
        "controller_options": {
            "template": {
                "template": "page.tpl",
                "is_catinclude": true
            },
            "zotonic_dispatch_file": "dispatch",
            "zotonic_dispatch_module": "mod_site_whatwebwhat"
        }
    },
    "status": "ok"
}
```

Creating URLs
-------------

URLs or page paths can also be requested using the `url_for` method. Here the name of the dispatch rule is padded, optionally with any extra arguments.

Example:

```bash
curl -k 'https://example.test:8443/api/model/dispatch/get/url_for/admin'
```

Returns:

```json
{"result":"/en/admin","status":"ok"}
```

Or, use `abs_url_for` if a complete URL with domain is needed:

```bash
curl -k 'https://example.test:8443/api/model/dispatch/get/abs_url_for/admin'
```

Returns:

```json
{"result":"https://example.com/en/admin","status":"ok"}
```

And for more complex URLs:

```bash
curl -k 'https://example.test:8443/api/model/dispatch/get/url_for/page?id=1234&slug=foo&a=1&bar=baz'
```

Returns:

```json
{"result":"/en/page/1234/foo?a=1&bar=baz","status":"ok"}
```

Here the selected dispatch rule was something like:

```erlang
{page, [ "page", id, slug ], controller_page, [ {template, "page.tpl"} ]}
```

Because `id` and `slug` are part of the path, they are included in the generated URL path, the other arguments are added as query parameters.

Available Model API Paths
-------------------------

| Method | Path pattern             | Description                                                                      |
| ------ | ------------------------ | -------------------------------------------------------------------------------- |
| `get`  | `/url`                   | Dispatch full URL from payload field `url`, returning matched rule/controller/options/bindings for this site only (`enoent` on mismatch or no match). No further lookups. |
| `get`  | `/url/+url`              | Dispatch full URL `+url`, returning matched rule/controller/options/bindings when it resolves to the current site; otherwise `enoent`. No further lookups. |
| `get`  | `/path`                  | Dispatch path from payload field `path`, returning matched rule/controller/options/bindings (`enoent` when no rule matches). No further lookups. |
| `get`  | `/path/...`              | Dispatch supplied path segments (percent-encoded and joined) and return matched rule/controller/options/bindings. |
| `get`  | `/url_for/+name/...`     | Generate relative URL for dispatch rule `+name` using payload args (path vars consumed by rule, remaining args as query params); `enoent` when rule is unknown. |
| `get`  | `/abs_url_for/+name/...` | Generate absolute URL for dispatch rule `+name` from `url_for` result and current site host/scheme; `enoent` when rule is unknown. |

`/+name` marks a variable path segment. A trailing `/...` means extra path segments are accepted for further lookups.

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2437?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/2437?language=en>)
