# Controllers

See **plug** for more info since controllers are plugs

External data uses string keys, internal data uses atoms

### Actions

All actions take two args:

* `conn`: a struct which holds data about requests
* `params`: request params(seems to everything like post body, query params, and more), Map with **string keys**&#x20;

Return values with `json/2`, `text/2`, and `html/2`

To let Phoenix views build resp, use `render/3`

lib/hello\_web/controllers/hello\_controller.ex

```elixir
defmodule HelloWeb.HelloController do
  use HelloWeb, :controller
  import Hello.Auth, only: [logged_in_user: 2]
  #pass through plug with a guard(like middleware)
  plug :logged_in_user when action not in [:new, :create]

  #params are combo of /m/:messenger and query params
  def show(conn, %{"messenger" => messenger}) do
    #gets :messenger from /hello/:messenger route
    #render(conn, "show.html", messenger: messenger) ORRRR
    conn
    |> assign(:messenger, messenger)
    |> render("show.html")
    #find and render show.html.eex in lib/hello_web/templates/hello (named after controller)
  end

  def redirect(conn, _params) do
      conn 
      |> put_flash(:info, "Redirecting")
      |> redirect(to: "/redirect_test")
      #      |> redirect(external: "google.com")
  end

  def get(conn, _params) do
    conn
    |> put_status(401)
    |> json(%{user: 1})
    # |> text("") #is empty 200 resp
  end
end
```

## Flashes

Add server messages

```elixir
conn
|> put_flash(:info, "Welcome to Phoenix, from flash info!")
|> put_flash(:error, "Let's pretend we have an error.")
|> render("index.html")
```

Instead of adding it to the html directly

templates/layout/app.html.eex

```
<meta name="flash-info" content="<%= get_flash(@conn, :info) %>" />
<meta name="flash-danger" content="<%= get_flash(@conn, :error) %>" />
```

App.js

```
useEffect(() => {
  const metaFlashInfo = document.querySelector("meta[name='flash-info']");
  const metaFlashDanger = document.querySelector("meta[name='flash-danger']");

  const toastIfContent = (message) => message && toast(message);

  toastIfContent(metaFlashInfo.content);
  toastIfContent(metaFlashDanger.content);
}, []);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openai.gitbook.io/code-cheatsheets/all/elixir/phoenix/controllers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
