<commit_before>---
title: Phoenix
category: Elixir
layout: 2017/sheet
weight: -1
updated: 2017-09-04
---

### Quick start

```bash
# Install Phoenix
mix local.hex
mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
```

```bash
# Create a new project
mix phx.new hello
```

Install Erlang, Elixir, Node.js, Postgresql first.
See: [Installation](https://hexdocs.pm/phoenix/installation.html) _(hexdocs.pm)_

### Directory structure

```
./
├── _build
├── assets/
│   ├── css/
│   ├── js/
│   ├── static/
│   └── node_modules/
├── config/
├── deps/
├── lib/
│   ├── hello/
│   └── hello_web/
│       ├── channels/
│       ├── controllers/
│       ├── templates/
│       ├── views/
│       ├── router.ex
│       └── gettext.ex
├── hello.ex
├── hello_web.ex
├── priv/
└── test/
```
{: .-box-chars}

See: [Adding pages](https://hexdocs.pm/phoenix/adding_pages.html) _(hexdocs.pm)_

### Migrations

[Ecto migrations cheatsheet](./phoenix-migrations)
{: .-crosslink}

```bash
$ mix ecto.gen.migration update_posts_table
  creating priv/repo/migrations/20160602085927_update_posts_table.exs
  ···
```

```elixir
create table(:documents) do
  add :title, :string
  add :title, :string, default: "Hello"
  add :body, :text
  add :age, :integer
  add :price, :float, precision: 10, scale: 2
  timestamps
end
```

### Routing

[Phoenix routing cheatsheet](./phoenix-routing)
{: .-crosslink}

```elixir
get "/", PageController, :index

resources "/users", UserController do
  resources "/posts", PostController
end
```

```elixir
user_post_path(:index, 17)     #=> /users/17/posts
user_post_path(:show, 17, 12)  #=> /users/17/posts/12
```

### Conn

[Phoenix conn cheatsheet](./phoenix-conn)
{: .-crosslink}

```elixir
conn.host          #=> "example.com"
conn.method        #=> "GET"
conn.path_info     #=> ["posts", "1"]
conn.request_path  #=> "/posts/1"
```

```elixir
conn
|> put_status(202)
|> html("<html><head>···")
|> json(%{ message: "Hello" })
|> text("Hello")
|> redirect(to: "/foo")
|> render("index.html")
|> render("index.html", hello: "world")
|> render(MyApp.ErrorView, "404.html")
```

### Ecto

[Ecto cheatsheet](./phoenix-ecto)
{: .-crosslink}

```bash
$ mix phx.gen.html \
    Accounts \       # domain
    Profile \        # schema
    profiles \       # table name
    email:string \
    age:integer
```

### Also see

- [Phoenix framework site](http://phoenixframework.org/) _(phoenixframework.org)_
- [Phoenix: getting started](https://hexdocs.pm/phoenix/overview.html) _(hexdocs.pm)_
<commit_msg>phoenix: fix arrows<commit_after>---
title: Phoenix
category: Elixir
layout: 2017/sheet
weight: -1
updated: 2017-09-04
---

### Quick start

```bash
# Install Phoenix
mix local.hex
mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
```

```bash
# Create a new project
mix phx.new hello
```

Install Erlang, Elixir, Node.js, Postgresql first.
See: [Installation](https://hexdocs.pm/phoenix/installation.html) _(hexdocs.pm)_

### Directory structure

```
./
├── _build
├── assets/
│   ├── css/
│   ├── js/
│   ├── static/
│   └── node_modules/
├── config/
├── deps/
├── lib/
│   ├── hello/
│   └── hello_web/
│       ├── channels/
│       ├── controllers/
│       ├── templates/
│       ├── views/
│       ├── router.ex
│       └── gettext.ex
├── hello.ex
├── hello_web.ex
├── priv/
└── test/
```
{: .-box-chars}

See: [Adding pages](https://hexdocs.pm/phoenix/adding_pages.html) _(hexdocs.pm)_

### Migrations

[Ecto migrations cheatsheet](./phoenix-migrations)
{: .-crosslink}

```bash
$ mix ecto.gen.migration update_posts_table
  creating priv/repo/migrations/20160602085927_update_posts_table.exs
  ···
```

```elixir
create table(:documents) do
  add :title, :string
  add :title, :string, default: "Hello"
  add :body, :text
  add :age, :integer
  add :price, :float, precision: 10, scale: 2
  timestamps
end
```

### Routing

[Phoenix routing cheatsheet](./phoenix-routing)
{: .-crosslink}

```elixir
get "/", PageController, :index

resources "/users", UserController do
  resources "/posts", PostController
end
```

```elixir
user_post_path(:index, 17)     # → /users/17/posts
user_post_path(:show, 17, 12)  # → /users/17/posts/12
```

### Conn

[Phoenix conn cheatsheet](./phoenix-conn)
{: .-crosslink}

```elixir
conn.host          # → "example.com"
conn.method        # → "GET"
conn.path_info     # → ["posts", "1"]
conn.request_path  # → "/posts/1"
```

```elixir
conn
|> put_status(202)
|> html("<html><head>···")
|> json(%{ message: "Hello" })
|> text("Hello")
|> redirect(to: "/foo")
|> render("index.html")
|> render("index.html", hello: "world")
|> render(MyApp.ErrorView, "404.html")
```

### Ecto

[Ecto cheatsheet](./phoenix-ecto)
{: .-crosslink}

```bash
$ mix phx.gen.html \
    Accounts \       # domain
    Profile \        # schema
    profiles \       # table name
    email:string \
    age:integer
```

### Also see

- [Phoenix framework site](http://phoenixframework.org/) _(phoenixframework.org)_
- [Phoenix: getting started](https://hexdocs.pm/phoenix/overview.html) _(hexdocs.pm)_

{'fraction_non_alphanumeric': 0.2009033778476041, 'fraction_numerical': 0.019638648860958365, 'mean_word_length': 3.487224669603524, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4455392', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1985, 'n_words': 448}