Quick start

# Install Phoenix
mix local.hex
mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
# Create a new project
mix phx.new hello

Install Erlang, Elixir, Node.js, PostgreSQL first. See: Installation (hexdocs.pm)

Directory structure

./
โ”œโ”€โ”€ _build
โ”œโ”€โ”€ assets/
โ”‚   โ”œโ”€โ”€ css/
โ”‚   โ”œโ”€โ”€ js/
โ”‚   โ”œโ”€โ”€ static/
โ”‚   โ””โ”€โ”€ node_modules/
โ”œโ”€โ”€ config/
โ”œโ”€โ”€ deps/
โ”œโ”€โ”€ lib/
โ”‚   โ”œโ”€โ”€ hello/
โ”‚   โ”œโ”€โ”€ hello.ex
โ”‚   โ”œโ”€โ”€ hello_web/
โ”‚   โ”‚   โ”œโ”€โ”€ channels/
โ”‚   โ”‚   โ”œโ”€โ”€ controllers/
โ”‚   โ”‚   โ”œโ”€โ”€ templates/
โ”‚   โ”‚   โ”œโ”€โ”€ views/
โ”‚   โ”‚   โ”œโ”€โ”€ router.ex
โ”‚   โ”‚   โ””โ”€โ”€ gettext.ex
โ”‚   โ””โ”€โ”€ hello_web.ex
โ”œโ”€โ”€ priv/
โ””โ”€โ”€ test/

See: Adding pages (hexdocs.pm)

Migrations

$ mix ecto.gen.migration update_posts_table
  creating priv/repo/migrations/20160602085927_update_posts_table.exs
  ยทยทยท
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

get "/", PageController, :index

resources "/users", UserController do
  resources "/posts", PostController
end
user_post_path(conn, :index, 17)     # โ†’ /users/17/posts
user_post_path(conn, :show, 17, 12)  # โ†’ /users/17/posts/12

Conn

conn.host          # โ†’ "example.com"
conn.method        # โ†’ "GET"
conn.path_info     # โ†’ ["posts", "1"]
conn.request_path  # โ†’ "/posts/1"
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

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

Also see

0 Comments for this cheatsheet. Write yours!