# Add an HTTP controller

Source: https://zotonic.com/docs/1540/add-an-http-controller

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

Language: en

Use controller_template when a dispatch rule only needs to render a template. Write a custom controller when the endpoint needs additional request processing or response behavior.

Use `controller_template` for a page that only renders a template. For a public, read-only plain-text endpoint, create `src/controllers/controller_garden_hello.erl` in the active Garden application:

```erlang
-module(controller_garden_hello).
-export([content_types_provided/1, process/4]).

-spec content_types_provided(Context) -> {Types, Context}
    when Context :: z:context(), Types :: list().
content_types_provided(Context) ->
    {[{<<"text">>, <<"plain">>, []}], Context}.

-spec process(Method, Accepted, Provided, Context) -> {Body, Context}
    when Method :: binary(), Accepted :: term(), Provided :: term(),
         Context :: z:context(), Body :: binary().
process(_Method, _Accepted, _Provided, Context) ->
    {<<"Welcome to the garden">>, Context}.
```

Add a rule to the application's `priv/dispatch/garden` list:

```erlang
{garden_hello, ["garden-hello"], controller_garden_hello, []}
```

Compile, refresh discovery, and request `/garden-hello` on the site's hostname. Expect a plain-text response containing the greeting. Check dispatch if it returns another page. The rule above is one tuple inside the dispatch list, not an entire dispatch file.

This example deliberately returns public constant text. Private data needs explicit authorization callbacks and model checks; a write endpoint also needs method handling, input validation, and protection against unauthorized requests. Consult <a href="/id/1537" class="doc-reference doc-reference-controller"><code>controller#controller_template</code></a> for a built-in alternative and <a href="/id/1875" class="doc-reference doc-reference-controller"><code>controller#controller_api</code></a> for model-based HTTP access.

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/1540?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/1540?language=en>)
